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 a field in a view from a different model?

By
Luigi Sison
on 1/20/15, 6:26 PM 1,224 views

Use Case:

Model 1 contains 2 fields: ID and Name

Model 1 has a one to many relationship with Model 2.

View A displays the contents of Model 2.

How can View A also display Name from Model 1?

model 1 and model 2 from same module?

Demirel yakito
on 1/20/15, 11:48 PM

Yes

On Tuesday, January 20, 2015, David <krevatharamanan@gmail.com> wrote:

model 1 and model 2 from same module?

--
David
Sent by Odoo S.A. using Odoo about Forum Post How to display a field in a view from a different model?


--

Best regards,

Luigi Sison | Implementation Partner
moxylus
phone +1 612 281 2551
824 Phoenix Lane | Foster City, CA 94404 | USA
lsison@moxylus.com | Our Website | My LinkedIn Profile


Moxylus, Luigi Sison
on 1/21/15, 1:44 AM

Axel, Thank you for the code. I need to implement this in Odoo SaaS. How do you specify 'name_rel': fields.related('model1_id', 'name', type='char', string='Model 1 Name' in Settings>Technical>Database Structure>Models ?

Moxylus, Luigi Sison
on 1/21/15, 12:29 PM
2

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 1/20/15, 11:59 PM

Hello Luigi Sison,

If it is normal xml view for form and tree than just put name of your field (many2one) of that model, if you have defined name field on that model than name value will be display. you cannot display other fields. If you want to display multiple field display for many2one than you need to override name_get() of that model.

and yes, if it is about q-Web report than you can follow the answer given by Jeff Beidler .

Hope this will help.

Regards,

Anil.

 

 

0
Jeff Beidler
On 1/20/15, 6:40 PM

Try something like <span t-field="o.mod1_id.name">, where "mod1_id" is the name of your relation field to Model 1.

0

Axel Mendoza

--Axel Mendoza--
10318
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 1/21/15, 12:00 AM

In every one2many relationship field there is a many2one field in the other side of the relation, so you should have in Model 2 a many2one field to Model 1. To display the name of Model 1 in a form view of Model 2 you need to declare the name as a related field in Model 2 class definition, something like this:

class model1(osv.osv):
    _name = 'model1'
    
    _columns = {
        'name': fields.char('Name', size=64, required=True),
        'model2_ids': fields.one2many('model2', 'model1_id', string='Models 2'),
    }
model1()

class model2(osv.osv):
    _name = 'model2'
    
    _columns = {
        'name': fields.char('Name', size=64, required=True),
        'model1_id': fields.many2one('model1', string='Model 1'),
        'name_rel': fields.related('model1_id', 'name', type='char', string='Model 1 Name')
    }
model2()    

Then in your view of Model 2 just add 

<field name="name_rel"/>

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: 1/20/15, 6:26 PM
Seen: 1224 times
Last updated: 3/16/15, 8:10 AM