Skip to Content
Menu
This question has been flagged
1 Reply
3315 Views

i got this class

 class estimation_cout(models.Model):

 _name = "estimation.cout"

 product_id = fields.Many2one('product.product', string='Produit')

 bom_id = fields.Many2one('mrp.bom', string='Nomenclature')

 when i select a product i want to get his bom like in MRP can u please tell me what and how i should write the onchage to make that works

Avatar
Discard

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]

Best Answer

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

Avatar
Discard
Related Posts Replies Views Activity
2
Dec 22
17982
5
Oct 23
5609
2
Apr 19
2026
3
Apr 19
2918
2
Feb 20
3116