跳至内容
菜单
此问题已终结
3 回复
52684 查看

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?

形象
丢弃
最佳答案

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.

最佳答案

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
5月 19
4643
2
1月 18
8798
1
2月 19
5160
1
9月 25
1523
3
7月 25
7382