Odoo 10 - 链接记录的计数值

Odoo 10 - Counting values of linked records

在 Odoo 10 中,我创建了自己的自定义应用程序(使用新的工作室功能),但是我在尝试计算属于不同视图的记录之间的数据时遇到了 运行 问题。

在我有两个模型(模型 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

保存服务器操作并执行它。

代码应该是不言自明的,如有任何问题,请随时在下面提问和评论。