Optimise Development with Environment Methods in Odoo 17

Muhammad Abdullah Arif
4 min readMay 20, 2024


In Odoo, the Environment is a crucial aspect that stores contextual data used by the Object-Relational Mapping (ORM) system. By leveraging predefined methods, developers can access and modify this data to enhance their code’s efficiency and readability. In this post, we’ll explore some commonly used environment methods in Odoo 17 and how they can optimize your development process.

Useful Environment Methods

env.ref(xml_id, raise_if_not_found=True)

The ref() method is invaluable for fetching records using XML IDs. By specifying an external identifier (e.g., module_name.id), this function returns the corresponding recordset. The raise_if_not_found parameter, which defaults to True, ensures that a ValueError is triggered if the XML ID is not found. This method enhances code reliability by providing a straightforward way to reference records.

record = self.env.ref('module_name.record_id')


This method checks if the current environment operates in superuser mode. It’s particularly useful for functionalities that need to be restricted to superusers.

if self.env.is_superuser():
# Execute superuser-specific code


The is_admin method verifies if the current user has the “Access Rights” group or superuser status. This is essential for customizing features or views based on administrative roles.

if self.env.is_admin():
# Execute admin-specific code


This method checks if the current user has the “Settings” group or is a superuser. It’s beneficial for controlling access to system configurations.

if self.env.is_system():
# Execute system-level code

Altering The Environment

Modifying the environment allows you to change the functionality and behavior of Odoo applications. Here are several methods to adjust the environment:


Use this method to modify the context and include additional information within the environment. It seamlessly merges the provided context, overriding existing keys and adding new ones.

records = self.env['model.name'].with_context(lang='fr_FR').search([])

Model.with_user(res_user or ID)

This method is useful when you need to execute a method or access data with a different user context. It accepts the user database ID or recordset as a parameter.

records = self.env['model.name'].with_user(self.env.ref('base.user_admin')).search([])

Model.with_company(res_company or ID)

Certain fields in Odoo are company-dependent. By using with_company, you can fetch values specific to a particular company.

records = self.env['model.name'].with_company(self.env.ref('base.main_company')).search([])


To execute tasks with superuser privileges, you can utilize sudo() within the environment. However, be cautious as it bypasses all security checks, which may lead to unintended data access across different companies in multi-company environments.

records = self.env['model.name'].sudo().search([])

Caution: Using sudo() can cause data access to cross the boundaries of record rules, potentially mixing records meant to be isolated. Use it judiciously to avoid unexpected results.


Utilizing Odoo’s environment methods helps you write cleaner, more adaptable code while controlling execution flow efficiently. By ensuring tasks run with proper access rights, these methods contribute to the creation of secure and robust Odoo applications. For the most up-to-date information, always refer to the official Odoo documentation.

Click on the link below to discover a wealth of knowledge and explore a variety of engaging topics.

Medium Profile: Muhammad Abdullah Arif — Medium

Stay Up-to-Date with Muhammad Abdullah Arif’s Latest Publications — Subscribe Now! (medium.com)

If you wish to offer your support, kindly click on the link below to treat me to a coffee! ☕️😊


I wish you all the best in your future endeavours.



Muhammad Abdullah Arif

Python developer. The facts are the facts but opinions are my own.