Odoo Help


get current user's company id

on 12/1/15, 9:10 AM 9,857 views

in a model i defined a company_id this way:

company_id = fields.Many2one(
    default=lambda self: self.env.user.company_id 

In a new document edit form the company id is correctly selected, but when I click on "company id" selection, I get this error:

Uncaught Error: AttributeError: object has no attribute 'company_id'

The "self" object appears to be empty, when logged.

I copied the definition of company_id from one of many core or OCA modules that uses it.

Does it rely on context?

And how could I get the id of the current user and is company_id?

On 10/14/16, 3:01 PM

Using new api: default=lambda self: self.env.user.company_id.id

self.env.user.company_id - recordset

company_id = fields.Many2one - integer=id of another record



| 6 5 7
Lodz, Poland

On 12/2/15, 10:48 AM

Try like this:

default=lambda self: self.env['res.company']._company_default_get('your.module')


probably correct code is slightly different, see https://github.com/odoo/odoo/issues/10906

 default=lambda self: self.env['res.company'].browse(self.env['res.company']._company_default_get('your.module'))

I can confirm it works just great. I'm using it to retrieve the company's default currency.

Aron Lorincz
on 12/4/15, 11:05 AM

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.


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

1 follower(s)


Asked: 12/1/15, 9:10 AM
Seen: 9857 times
Last updated: 10/17/17, 10:27 AM