This question has been flagged
1 Reply
6571 Views

By default, the mrp.bom module refers each bill of material (bom) to a parent product through the product_id field, which establishes a many2one relation to the product.product model.

Now, I would like to refer to and display fields belonging to the parent product in the view of the bom itself. How is this best achieved?

Avatar
Discard

Do u want a form view to display all the BOM relavant to particular product..?

is your need is to display product informations of "Product" field in BOM? ie product for which we define BOM.

Author

My need is -- in the bom view -- to display information taken from the product to which the particular bom viewed is related.

Best Answer

That's the purpose of related fields. On the bom object, create a related field poiting to the field you wan to display.

Here is in an old example from the doc. The purpose is to display country_id by going through state_id :

'country_id': fields.related( 'state_id', 'country_id', type="many2one", relation="res.country", string="Country", store=False)

You can the use field 'country_id' as usual.

Avatar
Discard
Author

Thank you. I have tried playing around with related fields but could not make it work. the browser-interface editor does not have an option for related fields, so I wonder if these are still supported? If they are, which python file should be edited to create the new field?

I think you need to get some development done for this. I don't think you can add a related field from the web interface. First, you need to inherit the model you want to add the field to (in your case 'mrp.bom'), and add the related column. Second, you need to add the field in the views of 'mrp.bom' that you want this field to appear. Third, if you insist that the value of the field need to change if you change the parent field, you need to develop an on_change mechanism to make it happen. addons/mrp/mrp.py provides some example on how to do this, like company_id field of mrp.routing.workcenter is a related field. Answering your questions on which python file should be edited: if you can familiarize yourself with the Odoo/OpenERP ORM framework and how it works, I would advise do it in a new module. If not, you can just edit addons/mrp/mrp.py for the time being. Also, to alter the view, you need to inherit the views from addons/mrp/mrp_view.xml. Hope this help.

Author

Thank you for your replay, John D. I have tried experimenting with editing the mrp.bom but run into a couple of problems: 1) I am a bit unsure regarding the syntax of the fields.related statement. Say I want to relate the field 'bar' in mrp.bom to the field 'foo' in product.product. Would the syntax be something like: "fields.related('foo','bar',type='char', relation='product.product', etc. etc." or just: "fields.related('foo',type='char', relation='product.product',etc. etc." or something else entirely? 2) When viewing the bom module in the browser after adding the new field 'bar', I get an error message stating the the column bar does not exist. One way to fix this is to add the column bar directly to the underlying sql database, but then then the relation between 'foo' and 'bar' is not implemented. Anyway, it seems like bad pratice to add a field to a database relation which essentially just copies information from another relation. Wouldn't there be a way to just *display* information pertaining to the product.product module in the view of the mrp.bom module?

The field should be something like 'bar': fields.related('product_id','foo',....). Where 'product_id' is the "parent" model. For complete sample of ...., you can search some samples in addons folder. In the mrp/mrp.py itself (line 124): 'company_id': fields.related('routing_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True). string will became the label of the field, store indicates whether the field should be stored or not (usually related field is not stored as you can still easily access it through the "parent" model"), readonly will control whether the field will be displayed as readonly or not in the view (usually related fields are displayed as readonly as it only displays the information of the selected "parent" if you need to change the information, you go to the "parent" model).

Author

Thanks again. That makes it clearer. I still get the error: "ProgrammingError: column mrp_bom.bar does not exist LINE 1: ...ency",mrp_bom."active",mrp_bom."product_rounding",mrp_bom."b...", though, and I am not sure how to best handle this.

If you have added the column you need to restart the server and upgrade the module that you have changed.

Author

Thank you very much, John. This solved the problem for me perfectly.