本篇文章原文来自:https://www.cnblogs.com/ygj0930/p/10826000.html
odoo中,一般情况下我们只会使用compute计算字段进行计算,并且依赖于另外字段进行计算,比如:
# 定义一个float类型的字段
total = fields.Float(compute='_compute_total')
@api.depends('value', 'tax')
def _compute_total(self):
for record in self:
record.total = record.value + record.tax
可以看出,total的值是根据value和tax的值相加得到的;但是如果只有value值和total值,而没有tax值,此时又该怎么得到tax?按理说tax的值为tax = total - value, 实现这个逻辑的方法就是利用inverse属性值指向的方法:
total = fields.Float(compute='_compute_total', inverse='_set_tax')
@api.depends('value', 'tax')
def _compute_total(self):
for record in self:
record.total = record.value + record.tax
def _set_tax(self):
for record in self:
record.tax = record.total - record.value
以下是odoo文档给出的解释:
Computed fields are readonly by default. To allow setting values on a computed field, use the parameter. It is the name of a function reversing the computation and setting the relevant fields:inverse
意思是默认情况下计算字段是只读的,要允许在计算字段上设置值,请使用该参数。inverse它是一个可以反转计算并设置相关字段的函数的名称