Working With env Variable
The ORM stores a variety of context data in the Environment, such as the database cursor
(for database queries), the current user (for access permissions verification), the
current context (for storing arbitrary metadata), and caches.
All recordsets have an environment, can be accessed using env and gives access to:
- the current user (user)
- the cursor (cr)
- the superuser flag (su)
- or the context (context)
The environment is inherited when a recordset is formed from another recordset.To get an
empty recordset in a model and query that model, utilise Environment.
>>> 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)
It will return the record corresponding to the xml_id
Eg:
>>> self.env.ref('base.user_admin')
res.users(2,)
env.user
It will return the current user of the environment. The returned output will be a record
from res.users
env.lang
It will return the language code for the current environment. It will be of str type.
env.company
It will return the current company and will be a record of model res.company.
Fallback to the current user's main company if the context (allowed company ids) is not
specified. When the allowed_company_ids context key content is invalid or unauthorized,
it will generate an AccessError.
env.companies
Return a record set of the enabled companies by the user.
Fallback to the current user's main company if the context (allowed company ids) is not
specified. When the allowed_company_ids context key content is invalid or unauthorized,
it will generate an AccessError.
Altering the Environment
with_context([context][, **overrides]) → records
This method creates a new version of this recordset with an additional context.
Eg:
# current context is {'key1': True}
result1 = records.with_context({}, key2=True)
# result1._context is {'key2': True}
result2 = records.with_context(key2=True)
# result2._context is {'key1': True, 'key2': True}
with_user(user)
Return a new version of this recordset in non-superuser mode connected to the specified,
unless the user is the superuser
with_company(company)
Return a new version of this recordset with a modified context, such that:
result.env.company = company
result.env.companies = self.env.companies | company
Parameters:
company (res_company or int) – main company of the new environment.
When using an unauthorized company for a current user, accessing the company(ies) on the
environment may trigger an AccessError if not done in a sudoed environment.
with_env(env)
Return a new version of this recordset attached to the provided environment.
Parameters:
env (Environment)
sudo([flag=True])
Returns a new version of this recordset with superuser mode enabled or disabled,
depending on the flag. The superuser mode does not change the current user and simply
bypasses access rights checks.
SQL Execution
The cursor for the current database transaction is represented by the cr attribute on
environments.
It allows you to run SQL directly instead of using the ORM, which is useful for queries
that are difficult to define using the ORM (e.g., complex joins) or for performance
reasons:
self.env.cr.execute("some_sql", params)
Because models all use the same cursor and the Environment contains a variety of caches,
these caches must be invalidated when changing the database in raw SQL, or else further
model usage would become incoherent. When using CREATE, UPDATE, or DELETE in SQL,
clearing caches is required, but not when using SELECT (which simply reads the
database).
The invalidate cache() method can be used to clean out caches.
invalidate_cache(fnames=None, ids=None)
Invalidate the record caches after some records have been modified. If both fnames and
ids are None, the whole cache is cleared.
Parameters
- fnames – The list of modified fields, or None for all fields.
- ids – The list of modified record ids, or None for all.