Hi,
See this onchange for the product_id field in the function in Manufacturing Order model.
@api.onchange('product_id', 'picking_type_id', 'company_id')
def onchange_product_id(self):
""" Finds UoM of changed product. """
if not self.product_id:
self.bom_id = False
else:
bom = self.env['mrp.bom']._bom_find(product=self.product_id, picking_type=self.picking_type_id, company_id=self.company_id.id)
if bom.type == 'normal':
self.bom_id = bom.id
self.product_qty = self.bom_id.product_qty
self.product_uom_id = self.bom_id.product_uom_id.id
else:
self.bom_id = False
self.product_uom_id = self.product_id.uom_id.id
return {'domain': {'product_uom_id': [('category_id', '=', self.product_id.uom_id.category_id.id)]}}
You can change little changes here and use the same in your case.
Thanks
Onchange many2one filed in odoo: https://goo.gl/CBP9og
Try like below
@api.onchange("product_id")
def _onchange_product_id(self):
if not self.product_id:
self.quantity = False
self.bom_id = False
self.order_line = False
elif self.product_id:
bom = self.env["mrp.bom"].search([
("product_tmpl_id", "=", self.product_tmpl_id.id)])
if bom:
self.bom_id = bom[0]