场景:
当字段被设置为只读时,该字段由其他字段进行改变带出来。常规现象是无法保存到数据库的。
1,实现通过页面某字段数据关联出对应数据表的其他字段的数据;
2,将页面数据进行计算并保存到数据表
相关属性:
onchange + related + readonly + store
实现方法:
方法一、对于直接可以关联过来的字段,采用related 属性将字段属性关联过来,设置readonly=True,并通过store=True进行保存,代码片段如下:
cus_qty_total = fields.Float(string='整托数', digits=(12, 0),
related='quotation_id.basic_qty_total', readonly=True, store=True,)
方法二,对于model中某些字段,不能直接依赖related 进行关联,只能通过onchange进行取数并关联到字段上,处理方法需要model + views配合处理,代码片段如下:
model部分:
cus_selling_price = fields.Float(string='销售单价', digits=(12, 2),)
...
@api.onchange('quotation_id')
def onchange_quotation_id(self):
''' 当商品型号行的型号变化时,带出型号上的销售单价'''
if self.quotation_id:
self.cus_selling_price = self. cus_selling_price #todo 关联去拿销售单价
...
views部分:
<field name="cus_selling_price" readonly="1" force_save = "1"/>