Working with env Variable
In the Odoo environment, the ORM (Object-Relational Mapping)
maintains essential context data such as the database cursor for
executing queries, the current user for access rights validation,
the context for holding metadata, and internal caches.
Each recordset is linked to an environment, accessible via env, which
provides access to:
- The current user (user)
- The database cursor (cr)
- The superuser mode flag (su)
- The execution context (context)
When a new recordset is derived from an existing one, it inherits the
same environment. The environment can also be used to create an
empty recordset for a specific model and to perform queries on that
model.
>> self.env['sale.order']
sale.order()
>>> self.env['sale.order'].search([('state','=','draft')])
sale.order(4,7,9,17,33)
env.ref(xml_id, raise_if_not_found=True)
Returns the record associated with the given XML ID.
>> self.env.ref('base.user_admin')
res.users(2)
env.user
Returns the current user as a record from the res.users model.
env.lang
Returns the current language code in use. The return type is a string
(str).
env.company
Returns the current company as a record from the res.company model.
If the allowed_company_ids key is not set in the context, it
defaults to the main company of the current user. If the context
contains invalid or unauthorized company IDs, an AccessError is
raised.