希望您一切都好。我需要你的一点帮助。我在Onchange方法上做了一个功能。我想要,如果我更改字段即final_unique_id,那么它将创建一个位于不同模块中的模型。但是它不能在onchange领域工作。它正在刷新页面并保存表单。请帮帮我。
@api.multi
@api.onchange('final_unique_id')
def finish_serial_no(self):
vals = {
'name': self.production_id.name,
'work_order_id':self._origin.id,
'product_id':self.product_id.id,
'unique_number':self.final_unique_id,
'date': datetime.datetime.now(),
'info_date': datetime.datetime.now(),
'user': self.env.user.id,
'status': 'Done',
}
res = self.env['info.module'].create(vals)
您尝试做的事是错误的,您正在'info.module'
中创建一条记录与当前模型有many2one work_order_id
的关系,您需要知道在保存记录之前,id是NewId
的实例; wife是虚拟类,因此在创建记录时,它不会附加到当前记录,因为它不是尚未保存,它没有ID(创建记录时,self._origin.id
将不起作用,只有保存后才可以)
您说的是保存后会创建记录并刷新页面,我假设您在one2many
中有一个'info.module'
字段[如果您没有创建它,不需要用户查看它,只需将其隐藏在视图中即可],我们假设它的名称为info_module_id
:
@api.onchange('final_unique_id')
def finish_serial_no(self):
vals = {
'name': self.production_id.name,
# 'work_order_id':self._origin.id, this will be handled by the one2many when you save the record
'product_id': self.product_id.id,
'unique_number': self.final_unique_id,
'date': datetime.datetime.now(),
'info_date': datetime.datetime.now(),
'user': self.env.user.id,
'status': 'Done',
}
# Use `new` instead of create this will create the record but doesn't save it in database.
self.ifo_module_id |= self.env['info.module'].new(vals) # add the record to one2many using `|` operator
不建议使用onchange
方法创建记录,因为用户始终可以单击丢弃按钮。希望对您有帮助