gpt4 book ai didi

python - 如何通过 onchange 方法在不同模型上创建记录?

转载 作者:行者123 更新时间:2023-12-04 17:30:40 24 4
gpt4 key购买 nike

希望你一切顺利。我需要你帮个小忙。我在Onchange Method 上做了一个函数。我想,如果我更改字段,即 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 的一个实例,女巫是一个虚拟类,因此当您创建记录时,它不会附加到当前记录,因为它不是尚未保存它没有 ID(self._origin.id 创建记录时不起作用,只有在保存时才有效)

你是说它在保存后创建记录并刷新页面 我假设您有一个 one2many 字段到 'info.module'[如果你没有创建它,如果你不需要用户看到它只是将它隐藏在 View 中],我们假设它被称为 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 方法中创建记录,因为用户总是可以点击discard 按钮。希望对你有帮助

关于python - 如何通过 onchange 方法在不同模型上创建记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60015641/

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