Search code examples
formsmodelforeign-keysodooformview

Geting user details using foreign key from res.users in Odoo


I have a model Employee and have included a foreign key to store the user object from the res.users table using the following code in my model:

user_id = fields.Many2one('res.users', string='user id', default=lambda self: self.env.user)

In my form, I am already capturing the user object.

Now, I want to display the user object's details (id, name, email) using the foreign key (that is the user object) in my form view.

The <field name="user_id"/> in my form view would display the user name of the stored object. I want to display different fields for id, email.


Solution

  • you can create a related field for user_id,

    x = fields.Char(related='user_id.email')
    

    like this you can access the all field values of related record.