๐๐ง๐ฏ๐ข๐ซ๐จ๐ง๐ฆ๐๐ง๐ญ ๐๐ง ๐๐๐จ๐จ
Unlocking Odoo's Contextual Power: Environment and Predefined Functions
Photo by Jess Bailey on Unsplash
The environment in Odoo is an object that stores various contextual data used by the ORM. This data includes:
uid: The current user, used to check access rights.
cr: The current database cursor (for database queries).
context: The current context, used to store arbitrary metadata.
su: Superuser mode.
It also holds a cache for records.
You can access the environment using the following method:
self.env
Output: <Environment object>
Predefined Functions
These functions can be accessed using env
:
ref(self, xml_id, raise_if_not_found=True)
Returns the record corresponding to the given 'xml_id'.
Example:self.env.ref('
mail.mt
_note')
is_system
Returns whether the current user has group 'Settings' or is in superuser mode.
Example:self.env.is
_system()
user
Returns the current user.
Example:self.env.user
company
Returns the current company.
Example:self.env.company
companies
Returns a recordset of the enabled companies by the user.
Example:self.env.companies
lang
Returns the current language code.
Example:self.env.lang