在 Odoo 10 中,我创建了自己的自定义应用程序(使用新的工作室功能),但是我在尝试计算属于不同 View 的记录之间的数据时遇到了问题。
在这个场景中,我有两个模型(模型 A 和模型 B),其中模型 B 的记录通过多对一关系字段连接到模型 A 的记录。模型 B 中有一个字段用于计算输入的数值。
理想情况下,我想要实现的是某种形式的自动操作/服务器操作,循环遍历模型 A 中的记录,然后循环遍历模型 B 中的相关记录,将前面提到的数值字段的值加在一起,在继续处理下一条记录之前,将模型 A 中的字段值设置为等同的数字。
例如,字段名称是:
模型 A = x_a
- 模型 A ID 字段 = x_id_field
- 计算值的目标字段 = x_compute
模型 B = x_b
- many2one 字段 = x_a_id
- 数字字段 = x_value_field
我曾尝试使用自动操作来执行一些基本的 Python 代码(因为我认为这会像嵌套循环一样简单)但是由于不熟悉如何循环遍历记录,我的所有尝试都失败了odoo 以及如何访问其他模型及其记录(从 python)。
我将如何实现这一目标?
请您参考如下方法:
Ideally what I would like to achieve is have some form of Automated Action / Server Action, that loops through the records in Model A, then loops through related records in Model B adding together the values of the previously mentioned numerical value field and sets the value of a field in model A equal to the equated number, before continuing onto the next record.
使用相关文档模型创建自动操作 = 模型 a 在“操作”选项卡上创建一个服务器操作:
model_b_records = self.env['model_b'].search([('many2one_field', '!=', False)])
for record in model_b_records:
record.many2one_field.target_field_for_computed_value = record.numerical_field
保存服务器操作并执行它。
代码应该是不言自明的,如有任何问题,请随时在下面提问和评论。
