Odoo Help

0

How to display a field in a view from a different model?

By
Luigi Sison
on 1/20/15, 6:26 PM 2,111 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
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
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"/>

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)

Stats

Asked: 1/20/15, 6:26 PM
Seen: 2111 times
Last updated: 3/16/15, 8:10 AM