Help

2

get current user's company id

Avatar
Raffaele

in a model i defined a company_id this way:

company_id = fields.Many2one(
    'res.company',
    'Company',
    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?

Avatar
Discard
2 Answers
5
Avatar
Zbik
Best Answer

Try like this:

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


UPDATED:

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'))

1 Comment
Avatar
Discard
Avatar
Aron Lorincz
-

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

3
Avatar
Artem
Best Answer

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

Avatar
Discard