OpenERP 中的函数字段可以有内部函数的执行优先级吗?

Can functional fields in OpenERP have inner function's execution precedence?

假设我有两个字段和一个功能字段,比如说,module1:

class my_class(osv.osv):
    _name = 'my.class'

    def _subtotal(self, cr, uid, ids, field_name, args, context=None):
        return {obj.id: obj.quantity * obj.price for obj in self.browse(cr, uid, ids, context=context)}

    _columns = {
        'quantity': fields.float(string=_('Quantity')),
        'price': fields.float(string=_('Price')),
        'subtotal': fields.function(_subtotal, type='float', method=True, store=False, string=_('Subtotal'))
    }

模块2中的另一个字段和另一个功能字段(依赖于模块1):

class my_class(osv.osv):
    _name = 'my.class'
    _inherit = 'my.class'

    def _discounted(self, cr, uid, ids, field_name, args, context=None):
        return {obj.id: obj.subtotal - obj.discount for obj in self.browse(cr, uid, ids, context=context)}

    _columns = {
        'discount': fields.float(... params ...)
        'discounted': fields.function(_discounted, type='float', method=True, store=False)
    }

我能保证 _discounted 会在 _subtotal 之后执行吗?如果我在功能字段中使用 store=True,它有何不同?

:不要假设我可以将两个计算放在同一个模块中(问题的一个限制是功能在不同的模块中,如果不接受答案将不被接受提供的解决方案是在同一模块中加入此类计算)。这只是一个例子来说明我的问题和疑惑,而不是真实世界的代码。

从您的代码来看,module2 依赖于 module1。所以module1会先安装,然后继承。因为你设置了store="false",在这种情况下你不必担心哪个会先执行。

因为即使您的 module2 _discounted 方法首先被触发意味着它会调用 obj.subtotal 从而触发ORM 的 小计 函数字段。由于 store="false" 属性 你的 module1 _subtotal 被执行并且结果返回到你的 module2。

不要去 store="true"。在这种情况下,一旦值存储在数据库中,您需要指定一个触发字段来重新计算该值,否则对象中的所有字段都将充当触发字段。