我使用Odoo 10并有一个名为sq_cost的自定义字段。当我输入sq_cost时,它会更新standard_price。这适用于以下代码
@api.onchange('sq_cost')
def _onchange_sq_cost(self):
self.standard_price = (self.sqyards_per_box) * (self.sq_cost)
我的问题是我必须导入一个csv,其中一个要导入的字段是sq_cost。导入sq_cost时,计算不会运行。如果我键入它工作正常。
问题是,@api.onchange()
在视图级别工作,例如,
=> Edit a record in form view
=> You change value on a `@api.onchange()` field, `sq_cost` in this case
=> `@api.onchange` function is fired, some other field value is changed `standard_price` in this case
**But nothing is changed on the database yet, cause you are still on edit mode**
=> Changes are stored in database only if you press save button.
但是如果从CSV导入,视图级别没有任何更改,则直接在数据库中更改值,因此不会执行此过程。
解:
而不是在视图级别使用onchange
功能,您可以使用compute function
在standard_price
字段上使用@api.depends('sq_cost')
。这适用于数据库级别,因此每当导入sq_cost
值时都会计算值。要记住的事情:
** compute field is by default not stored, set `store=True`
** compute field is by default readonly, set `inverse='inverse_function'` to make this field editable