python - Odoo - 计算字段导入

标签 python odoo odoo-10 odoo-11

我使用 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/

相关文章:

python - Django TabularInline 字段在更新内联字段时出错

python - 如何创建 Pandas 时间戳对象?

python - 在 TreeView 中使用 "Group by"时如何对其他列求和?

python - 如何解决 Odoo 中的动态域问题?

python - 按 '],[' 拆分字符串,但保留括号

python - 事件 virtualenv 不工作

python - 如何从 odoo 8 中的另一个函数字段定义 one2many 字段?

odoo - 默认客户过滤器不显示客户 - odoo 10 中的多家公司

python - def 在odoo 10、python 中创建带有many2one 字段的函数

javascript - 如何从 AJAX 向 Odoo 10 自定义模块 Controller 发出 POST/GET 请求? (被 CORS 政策阻止)