我使用 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()
例如在 View 级别工作,
=> 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 导入, View 级别上不会发生任何更改,值会直接在数据库中更改,因此不会执行此过程。
解决方案:
您可以通过 @api.depends 在
。这适用于数据库级别,因此每当您导入 standard_price
字段上使用 compute function
,而不是在 View 级别使用 onchange
功能('sq_cost')sq_cost
时都会计算值
值(value)。需要记住的事情:
** 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
关于python - Odoo - 计算字段导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54908316/