Odoo Help

1

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

By
Luigi Sison
on 20/01/15 18:26 2.719 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 20/01/15 23:48

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 21/01/15 01:44

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 21/01/15 12:29
2
Anil R. Kesariya
On 20/01/15 23:59

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.

 

 

1
Axel Mendoza
On 21/01/15 00:00

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"/>

0
law rence
On 15/10/17 22:35

try to use this


from odoo import models, fields, api


class MyModel(models.Model):
    _name = "my.model"

    another_model_ids = fields.One2Many(
        comodel_name="another.model", inverse="my_model_id",
        string="Another Model Entries")


class AnotherModel(models.Model):
    _name = "another.model"

    my_model_id = fields.Many2One(
        comodel_name="my.model", string="My Model")
    number = fields.Integer(string="A Number")
    yet_another_model_id = fields.Many2One(
        comodel_name="yet.another.model", string="Yet Another Model")

    @api.multi
    def name_get(self):
        # with context flags you can implement multiple
        # possibilities of name generation
        # best example: res.partner
        res = []
        for another_model in self:
            res.append((another_model.id, "{} {}".format(
                another_model.number,
                another_model.yet_another_model_id.name)))
        return res


class YetAnotherModel(models.Model):
    _name = "yet.another.model"

    name = fields.Char(string="Name")
0
Jeff Beidler
On 20/01/15 18:40

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

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: 20/01/15 18:26
Seen: 2719 times
Last updated: 15/10/17 22:35