Dear,
I have many2one field that always saves null value on change of bom_id field, knowing that x.product_tmpl_id.id is not null
product_templ_id = fields.Many2one(
comodel_name='product.template', check_company=True, ondelete='cascade', store=True, readonly=False
)@api.onchange('bom_id')
def _onchange_bom_id(self):
for rec in self:
lst = []
for x in rec.bom_id.bom_line_ids:
values = {
'product_templ_id': x.product_tmpl_id.id,
'company_id': rec.env.company.id,
'product_qty': x.product_qty,
}
lst.append(values)
rec.lines = [(5, 0, 0)]
if lst:
rec.lines = [(0, 0, x) for x in lst]