๐„๐ง๐ฏ๐ข๐ซ๐จ๐ง๐ฆ๐ž๐ง๐ญ ๐ˆ๐ง ๐Ž๐๐จ๐จ

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

ย