Odoo Help

Welcome!

This community 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.

1

get current user's company id

By
Raffaele
on 12/1/15, 9:10 AM 3,264 views

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?

1
Artem
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

2

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 12/2/15, 10:48 AM

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

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

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/1/15, 9:10 AM
Seen: 3264 times
Last updated: 10/14/16, 4:12 PM