Search code examples
modelodoorecord

Odoo 11 : want to implement bills of raw materials depending on sale.order model


I am very new to Odoo and get stuck often. Now, I am really stucked with no solution in mind. My goal is to count consummed raw materials. The problem is that I want to initialise a model (here assessment.raw.material) with the product_id in mrp.bom.line. I tried with "default=", ".create()" but it doesn't work. I think that if I get all product_id of all raw materials, i can easily fill all column with sql queries. Can you help me? Or i get it wrong? Or Do you have better idea? Thank you. Sorry for my bad english.

class AssessmentRawMaterials(models.Model):
    _name = 'assessment.raw.materials'


    # get the product_id from mrp.bom.line
    # which is nomenclature of each
    # finished product
    product_id = fields.Many2one(
        string='Matières premières',
        comodel_name='mrp.bom.line',
        ondelete="no action",
        store=True
   )

    # get the product unit of measure
    # by calling the variable name of 
    # product_id
    product_uom_name = fields.Char(
        string=u'Unité de mesure',
        related='product_id.product_id.name'
   )

    # compute using sql query, 
    # long long 
    # inner join
    # from sale.order to mrp.bom.line
    raw_material_qty = fields.Integer(
       string=u'Quantité de matières premières',
       default=0
   )

Solution

  • If you have one-to-one like relation between bom line and assessment raw material and want to user the bom lines product information, use related fields:

    bom_line_id = fields.Many2one(
        string='Matières premières',
        comodel_name='mrp.bom.line',
        ondelete="no action")  # store not needed default True
    product_id = fields.Many2one(
        comodel_name='product.product',
        related='bom_line_id.product_id',
        store=True)  # store explanation for related fields after code
    

    On normal many2one fields the store parameter is per default True. On related fields you really need to decide what should happen in the database:

    1. setting store=True will create a new column on your assessment_raw_materials table and will Odoo tell to copy the value from product_product table everytime it changes. So it is a bit redundant but sometimes wished for.
    2. setting store=False will not create a new column, but instead Odoo will always get the value from product_product table.