Readonly field in onchange method
If you change the value of a readonly field in onchange method, on save it will not be stored in the database. In order to do that you would need to override create/write methods where you would need to update the values dictionary with this value before creation.
For example, you would need to update the readonly field “invoice_method” the depends on the “partner_id” field. If you change its value in the onchange_partner_id method
@api.onchange('partner_id')
def onchange_partner_id(self):
res = super(MrpRepair, self).onchange_partner_id()
self.invoice_method = self.partner_id.prepaid and 'b4repair' or 'after_repair'
return res
the new value will not be stored in the database on save. Instead, you would need to extend the create method and to update the “vals” dictionary before creation
@api.model
def create(self, vals):
if vals.get('partner_id'):
vals.update({'invoice_method': self.env['res.partner'].browse(
vals.get('partner_id')).prepaid and 'b4repair' or 'after_repair'})
return super(MrpRepair, self).create(vals)
总结:只读字段需要根据其他字段进行onchange的时候,直接在create方法中更新字段即可。
V12以上的版本可以直接xml上添加 force_save=True解决保存问题
本文介绍了在Odoo中如何正确地处理只读字段的更改并确保这些更改能够被保存到数据库中。针对只读字段依赖其他字段变化的情况,文章提供了具体的代码示例,展示了如何在创建或更新记录的方法中更新只读字段。
2210

被折叠的 条评论
为什么被折叠?



