'How to get current company id odoo 15?

I've try this:

[('company_id','=',company_id)]

self.env.company_id

self.env.user.company_id

But those all 3 ways are leaded to default company, but not current company.

enter image description here

Please help, thanks.



Solution 1:[1]

I suggest creating a related field that points to the current company or just creating a new stored computed field show_field_XY. In the _compute method you can access the context like this:

self.env.context.get('company_id')

Finally, just access that field in the domain and you should be good to go.

Solution 2:[2]

try this

current_company = self.env.user.company_id

Solution 3:[3]

As i check, if we use context to get current company, and in case we using multi company, we can only get the first company.

In that case, we should use company_ids to get all the companies of user instead.

Example:

['|',('company_id','in',company_ids),('company_id','=',False)]

Hope it's help.

Solution 4:[4]

Have you tried self.env.company ? You could use a separate field to determine this and use that in your xml..

active_company_id = fields.Many2one(
        "res.company",
        string="Company",
        default=lambda self: self.env.company,
    )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Josip Domazet
Solution 2 Shilal
Solution 3 fudu
Solution 4 NinjaBat