Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
3 Răspunsuri
52630 Vizualizări

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?

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează

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

Cel mai bun răspuns

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

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
2
mai 19
4582
2
ian. 18
8758
1
feb. 19
5137
3
iul. 25
7264
1
mai 25
1256