【发布时间】:2018-07-28 21:16:57
【问题描述】:
所以我想通过@onchange 函数在stock.picking 中自动创建一条移动线(move_lines)。这是我的功能:这只是一个小测试。当字段changed 的值发生变化时,我将其作为移动行中产品的ID (product_id),然后将该移动行附加到现有的move_lines 列表中。
NB1:move_lines 是 stock.picking 中的 One2many 关系。
NB2:product_id 在stock.move 中的声明:
product_id = fields.Many2one(
'product.product', 'Product',
domain=[('type', 'in', ['product', 'consu'])], index=True, required=True,
states={'done': [('readonly', True)]})
我的功能:
changed = fields.Integer('Changed')
@api.onchange('changed')
def _changed_onchange(self):
move_lines = []
for line in self.move_lines:
move_lines.append({'product_id': line.product_id.id or False,
'product_qty': line.product_qty or 0,
'name': line.product_id.name,
'product_uom': line.product_uom.id,
'date_planned': datetime.date.today(),
'date_expected': datetime.date.today()
})
move_lines.append({'product_id': self.changed,
'name': 'default',
'product_uom': 1,
'date_planned': datetime.date.today(),
'date_expected': datetime.date.today()
})
return {'value': {'move_lines': move_lines}}
如果我使用视图创建移动线然后保存,一切正常,但是当我更改字段的值以便函数插入新的移动线时,保存不起作用,我一直得到错误:
Odoo Server Error - Validation Error
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[object with reference: product_id - product.id]
有什么问题?
【问题讨论】:
-
您将 move_lines 作为将被忽略的字典列表传递,它将尝试使用默认值创建移动并导致“未正确设置必填字段”。检查我的答案。
标签: odoo erp odoo-11 odoo-view