This question has been flagged
7 Replies
30664 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?

Avatar
Discard

model 1 and model 2 from same module?

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


Author

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 ?

Best Answer

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.

 

 

Avatar
Discard

Hello Anil,
Is there no solution to display the internal information of a field Many2one in a form or tree view?
Like:
<field name="user_id.phone" />
or
<span>${user_id.phone}</span>
or in a way!?

Thanks

Best Answer

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

Avatar
Discard
Best Answer

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")
Avatar
Discard
Best Answer

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

Avatar
Discard