Environment in Odoo

The environment in Odoo is an object that stores various contextual data used by the ORM. This data includes:

  • the current user (user)
  • the cursor (cr)
  • the superuser flag (su)
  • the context (context)

Predefined functions

env.ref(xml_id, raise_if_not_found=True)

It will return the record corresponding to the xml_id


>>> self.env.ref('base.user_admin')


It will return the current user of the environment. The returned output will be a record from res.users


It will return the language code for the current environment. It will be of str type.

It will return the current company and will be a record of model


Return a record set of the enabled companies by the user.

Altering the Environment

with_context([context][, **overrides]) → records

This method creates a new version of this recordset with an additional context.

# 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}


Return a new version of this recordset in non-superuser mode connected to the specified, unless the user is the superuser


Return a new version of this recordset with a modified context, such that: = company
result.env.companies = self.env.companies | company


Return a new version of this recordset attached to the provided environment.

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:"some_sql", params)
# Odoo
The launch of our new website!