Developers can more easily access models, user information, context, permissions, and database operations with Odoo 19's enhanced Environment (env) system. The fundamental interface that developers use within models and business logic is the self.env object. The most practical and helpful environment techniques that you will use on a daily basis in Odoo 19 are broken down clearly below.
1. Core Environment Attributes in Odoo 19
These characteristics offer direct access to crucial runtime data.
- self.env.user – Get the Current User
Returns the logged-in user record. Useful for permission checks, role-based logic, and personalization.
- self.env.company – Active Company
Returns the currently active company. This value updates automatically when the user switches companies.
- self.env.companies – Allowed Companies
Returns all companies that the user has access to in a multi-company environment.
- self.env.context – Current Context Dictionary
A runtime dictionary that controls default values, language, timezone, active record IDs, and behavioral flags.
- self.env.cr – Database Cursor
Provides direct access to the database cursor for executing raw SQL queries. Recommended only when ORM methods are insufficient.
- self.env.lang – Language Code
Returns the current interface language code, derived from the context.
lang = self.env.context.get("lang")2. Permission & Mode Check Methods
These boolean helper methods are used to implement role-based and privilege-based logic in Odoo.
- self.env.is_superuser()
Returns True when the code is executed as the Odoo superuser (user ID = 1).
Bypasses record rules and access rights.
- self.env.is_admin()
Returns True when the current user belongs to the Administration: Access Rights group. Useful for admin-only functional checks.
- self.env.is_system()
Returns True when the user has Settings-level administrative privileges.
Typically used for system configuration or sensitive operations.
3. Model Access & Querying Methods
These are the most commonly used ORM entry points in Odoo for accessing and retrieving data.
- self.env['model.name'] – Access Any Model
This is the primary gateway to interact with any Odoo model using the ORM
It allows you to search, create, update, and delete records
- Returns a recordset
- Respects access rights and record rules
- Preferred method for most data operations
partners = self.env['res.partner'].search([])
- self.env.ref('module.xml_id') – Fetch Records via XML ID
Used to retrieve records that are defined via XML external IDs.
Commonly used for views, actions, email templates, and configuration records.
- Raises an error if the XML ID does not exist
- Ideal for referencing static or predefined data
- Safer and cleaner than searching manually
template = self.env.ref("mail.email_template_user_signup")- self.env.execute_query("SQL") – Safe SQL Helper
Odoo 19 introduces execute_query() as a safe, cache-aware SQL execution method.
- Safer than direct cursor usage (self.env.cr.execute)
- Automatically handles caching and environment context
- Best suited for read-only or performance-critical queries
- Should be used only when ORM is not practical
result = self.env.execute_query("SELECT count(*) FROM res_partner"4. Context & Permission Modifiers
These methods allow you to temporarily alter execution behavior such as context values, user permissions, and company scope—without permanently changing the environment.
records=self.env['res.partner'].with_context(active_test=False).search([])
Runtime behaviours like record filtering, default values, language, and timezone handling are managed by context values. Active_test, lang, tz, and default_field are common keys, and any context modification only affects the returned recordset and not the entire world.
sudo()Executes operations with superuser access, bypassing access rights and record rules.
secure_records = self.env['res.partner'].sudo().search([])
It must be used carefully to prevent security risks because it circumvents record rules and access rights. This method works best for background tasks, system-level procedures, or operations that are only admin-level.
with_company(company)Runs operations under a specific company in multi-company environments.
orders = self.env['sale.order'].with_company(2).search([])
It automatically modifies company-dependent fields after accepting either a company record or a company ID. In multi-company settings, this guarantees accurate behaviour and appropriate data isolation.
with_user(user_id)Runs the operation as a different user, applying that user’s access rights and rules.
data = self.env['res.partner'].with_user(15).search([])
It is helpful for permission simulation and delegated actions because it executes operations using the target user's access rights and record rules. Because it maintains security constraints, it is frequently a safer option than sudo().
All model and business logic in Odoo 19 is based on the self.env object, which offers a consistent method of accessing models, users, companies, context, permissions, and database operations while automatically respecting security rules and runtime behavior. Developers can guarantee data integrity and maintainable code by depending on ORM-based access.
With safer SQL helpers, more transparent permission checks, and adaptable context modifiers like with_context(), with_company(), and with_user(), Odoo 19 further enhances this system. These tools provide sophisticated control over execution flow without sacrificing security when used properly. All things considered, developing scalable, safe, and expert Odoo applications requires a thorough grasp of the environment API.
To read more about Useful Environment Methods in Odoo 18, refer to our blog Useful Environment Methods in Odoo 18.