Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
52536 Widoki

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?

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć

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

Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
maj 19
4541
2
sty 18
8712
1
lut 19
5096
3
lip 25
7040
1
maj 25
1163