Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to display fields from parent model?

By
philjun
on 11/6/14, 6:22 AM 1,124 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?

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

Anand
on 11/6/14, 6:26 AM

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

Baiju
on 11/6/14, 8:06 AM

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

philjun
on 11/18/14, 3:39 AM
0
Christophe HANON
On 11/6/14, 7:24 AM

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.

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?

philjun
on 11/18/14, 3:38 AM

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.

Ivan
on 11/18/14, 4:22 AM

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?

philjun
on 11/18/14, 6:01 AM

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).

Ivan
on 11/18/14, 7:01 AM

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.

philjun
on 11/18/14, 8:19 AM

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

Ivan
on 11/18/14, 11:49 PM

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

philjun
on 11/20/14, 3:58 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 11/6/14, 6:22 AM
Seen: 1124 times
Last updated: 3/16/15, 8:10 AM