如何使用功能字段的store参数? [英] How does one use the store parameter of function fields?

查看:106
本文介绍了如何使用功能字段的store参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试阅读文档,但这有点令人困惑.

I've tried reading the docs, but it's a bit confusing.

store还可以与其他任何字段类型一起使用吗?

Also, does store work with any other field types?

推荐答案

首先要回答第二个问题:relatedsparse字段都是function的子类,因此store 可以/应该与他们一起工作,但我没有尝试过.

To answer the second question first: The related and sparse fields are both subclasses of function so store may/should work with them, but I have not tried.

store参数背后的想法是告诉OpenERP是否可以记住并保存调用函数的结果,以避免再次调用它.

The idea behind the store parameter is to tell OpenERP if it is okay to remember and save the results of calling the function in order to avoid calling it again.

关于它的工作方式,让我们看下面的示例:

For how it works, let's look at the following example:

    'order_status': fields.function(
        _order_status,
        type='char',
        method=True,
        store= . . . ,
        string='Order Status',
        ),

默认情况下,storeFalse-意味着每次请求记录时都会对功能字段进行评估.

By default, store is False -- meaning that the function field will be evaluated everytime the record is requested.

但是,还有其他两个可能的值-Truetuple s的dict.

There are, however, two other possible values -- True, or a dict of tuples.

        store=True,

True易于理解,仅表示该值将被一次计算,存储一次,然后在每次记录更改时重新计算.

True is easy to understand, and simply means that the value will be calculated once, stored, and then recalculated every time that record changes.

        store={
            'model.table': (function, ['field1', 'field2', ...], priority),
            'another_model.table': (some_func, [], priority),
            },

tupledict既有些复杂又非常强大.有了它,我们可以告诉OpenERP 何时我们要重新计算该字段.

The dict of tuples is both somewhat complicated as well as very powerful. With it we can tell OpenERP when we want the field recalculated.

键是表,例如res.partnerproduct.product;三项元组中的第一项是要调用的函数,第二项是键表中要监视的字段的列表,最后一项是优先级或顺序,如果有多个函数,则在其中处理功能一个 1 .

The keys are tables, such as res.partner or product.product; the first item in the three item tuple is the function to call, the second items is a list of fields in the key table to monitor, and the last item is the priority, or sequence, to process the functions in if there are more than one1.

一个例子如下:

        store={
            'product.product': (_get_product_dependent_ids, ['name','price'], 20),
            'res.partner': (_get_partner_dependent_ids, ['login'], 10),
            },

向后工作,优先级(每个元组的最后一项)告诉我们res.partner元组将首先运行,因为它的优先级较低.

Working backwards, the priorities (last item of each tuple) tells us that the res.partner tuple will run first, as it has a lower priority.

中间的项目是要监视的字段列表:对于res.partner,OpenERP将监视login字段,并且每当login字段更改时,OpenERP都会调用_get_partner_dependent_ids;否则,将更改_get_partner_dependent_ids.同样,只要更改product.product记录的nameprice字段,OpenERP就会调用_get_product_dependent_ids 2 .

The middle item is the list of fields to monitor: for res.partner OpenERP will monitor the login field, and any time the login field is changed OpenERP will call _get_partner_dependent_ids; likewise, anytime the name or price fields for a product.product record is changed, OpenERP will call _get_product_dependent_ids2.

元组中的第一项是要调用的函数,这是棘手的部分.该函数的签名是:

The first item in the tuple is the function to call, and it is the tricky part. The signature of this function is:

def _get_ids(key_table, cr, uid, ids_of_changed_records, context=None):

请注意,key_table不是self!即使此函数可能是您的依赖类中的方法(例如,custom.table1),第一个参数也不是该表,但是该表在商店字典中被列为键-在我们的示例 3 中是product.productres.partner.

Note that key_table is NOT self! Even though this function is probably a method in your dependent class (for example, custom.table1), the first parameter is not that table, but is the table listed as the key in the store dictionary -- product.product or res.partner in our example3.

该功能应该做什么?应该在您的自定义表中返回所有记录ID的列表,这些记录ID需要重新计算该字段.

What is this function supposed to do? It is supposed to return a list of all the record ids in your custom table that need to have that field recalculated.

这是我的职能领域:

    'order_status': fields.function(
        _order_status,
        type='char',
        method=True,
        store={
            'fnx.pd.order': (_get_schedule_ids_for_order, ['state'], 20),
            },

和我的商店功能:

def _get_schedule_ids_for_order(fnx_pd_order, cr, uid, ids, context=None):
    if not isinstance(ids, (int, long)):
        [ids] = ids
    return [s.id for s in fnx_pd_order.browse(cr, uid, ids, context=context).schedule_ids]

字段定义告诉我们,只要在fnx.pd.order中的记录上更改state字段,就会使用具有statefnx.pd.order中记录的ID来调用_get_schedule_ids_for_order.字段已更改.

What the field definition tells us is that any time the state field is changed on a record in fnx.pd.order, _get_schedule_ids_for_order will be called with the ids of the records in fnx.pd.order that had their state field changed.

_get_schedule_ids_for_order查找更改的记录,获取链接的计划记录的ID,然后将其返回.

_get_schedule_ids_for_order looks up the changed record, gets the ids of the linked schedule records, and returns them.

脚注:

  1. priority字段对表中的每个字段对每个_get_ids()函数进行排序,而不仅仅是单个函数的_get_ids().当一个函数字段依赖于另一个函数字段时,这非常有用.

  1. The priority field sorts every _get_ids() function for every field in the table, not just the _get_ids() for an individual function. This is very useful when one function field depends on another.

如果字段列表为空,则对任何字段的任何修改都将导致调用该函数.

If the field list is empty, then any modification to any field will cause the function to be called.

如果您需要访问该函数中自己的表,可以这样做

If you need access to your own table inside the function you can do

self = key_table.pool.get('my_module_name_here.my_table_name_here')

这篇关于如何使用功能字段的store参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆