Odoo - 计算字段导入

问题描述 投票:1回答:1

我使用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时,计算不会运行。如果我键入它工作正常。

python odoo odoo-10 odoo-11
1个回答
0
投票

问题是,@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 functionstandard_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
© www.soinside.com 2019 - 2024. All rights reserved.