gpt4 book ai didi

python - 如何在 onchange 方法中复制一个 One2many 字段并修改它?

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:13 25 4
gpt4 key购买 nike

我想我做了一千次,但今天我无法实现并且有很多疑问。

目的

我需要将 One2many 字段的记录复制到我的 One2many 字段,但要更改列的值。此操作必须在 onchange 方法中执行。

场景

我在模特mrp.bom .它有一个名为 bom_line_idsOne2many 字段.它还具有名为 product_idMany2one 字段.

每次product_id更改,我必须自动填写 bom_line_ids当前 Material list 记录,数据来自 bom_line_ids其他 Material list 字段。但是这些记录不会完全一样,我需要修改一列。

示例

onchange 方法中,我需要复制记录集 mrp.bom.line(10, 11)到我的 One2many 字段,但在我的 One2many 字段中,两条记录都有字段 line_type值为 'standard' (源记录的 line_type 值无关紧要)。

我的尝试

我尝试了很多东西。最接近解决方案的尝试是这个。但是当当前的 Material list 记录还没有保存在数据库中时,这会失败,因为 _origin .错误说 bom_id是 NULL 并且是强制性的...问题是如果我不写 _origin ,该错误总是出现,当前 Material list 是否已经存在无关紧要。如果我删除 bom_id来自 default 的 key 字典,新行被添加到源 One2many 而不是我的...

@api.onchange('product_id')
def onchange_product_id(self):
if self.product_id and \
self.product_id.product_tmpl_id.bom_ids:
# Following lines are only for taking the source BoM
bom_ids = self.product_id.product_tmpl_id.bom_ids.filtered(
lambda r: not r.product_id)
bom_id = bom_ids[0] if bom_ids else False
# Following lines are for doing the copy
new_lines = self.env['mrp.bom.line']
for line in bom_id.bom_line_ids:
new_lines += line.copy(default={
'bom_id': self._origin.id,
'line_type': 'standard',
})
self.bom_line_ids = [(6, 0, new_lines.ids)]

结论

我认为我正在使它变得比实际更复杂,必须有一些更简单的解决方案...有谁知道如何做到这一点?

最佳答案

您可以尝试其他选项,例如在 bom.line 对象中添加“事件”字段。

有了它,您可以禁用以前的 bom 行并在 onchange 方法中添加新的 bom 行,而不会干扰任何其他流程。

PS:我没试过

编辑:

浏览行并准备一个列表 (0, 0, { values }) 以更新 one2many 字段。

@api.onchange('product_id')
def onchange_product_id(self):
if self.product_id and \
self.product_id.product_tmpl_id.bom_ids:
bom_ids = self.product_id.product_tmpl_id.bom_ids.filtered(
lambda r: not r.product_id)
bom_id = bom_ids[0] if bom_ids else False
new_lines = []
for line in bom_id.bom_line_ids:
vals = line.read()[0]
vals.update({
'line_type': 'standard',
})
vals.pop('bom_id', False)
new_lines.append((0, 0, vals))
self.bom_line_ids = new_lines

关于python - 如何在 onchange 方法中复制一个 One2many 字段并修改它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53865916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com