Enable Dark Mode!
overview-of-context-in-domain-odoo19.jpg
By: Sayed Mahir Abdulla KK

Overview of Context in Domain Odoo19

Technical Odoo 19 Odoo Enterprises Odoo Community

In Odoo, domains are used to control which records are shown by applying conditions, such as displaying only products from a specific category or invoices in a particular state.

However, these conditions are not always the same for everyone. They may depend on who is logged in, which company is active, or from where the view is opened. This is where context comes in. Context is a temporary set of extra information that Odoo carries during an operation. By using context values inside domains, you can make filters dynamic and situation-aware. This allows Odoo to automatically show the right data to the right user at the right time, without hard-coding the conditions.

Usage:

A normal domain looks like:

[('field_name', '=', value)]

When combined with context, the domain can use dynamic values from the environment:

[('company_id', '=', context.get('company_id'))]

Here, the domain will filter records based on the current company stored in the context. You can also pass context manually in actions, views, or fields to influence the domain behavior.

Examples:

1. Filter based on current user

[('user_id', '=', uid)]

2. Filter based on current company

[('company_id', '=', context.get('company_id'))]

or

[('company_id', 'in', context.get('allowed_company_ids', []))]

3. Filter based on active_id (current record)

[('partner_id', '=', context.get('active_id'))]

or (in case of multiple records selected)

[('partner_id', 'in', context.get('active_ids', []))]

4. Filter based on default values from context

[('state', '=', context.get('default_state', 'draft'))]

5. Filter based on search_default filters

In XML view

<filter name="my_records" domain="[('user_id', '=', uid)]"
       context="{'search_default_my_records': 1}"/>

6. Multi-company filtering

['|', ('company_id', '=', False),
     ('company_id', 'in', context.get('allowed_company_ids', []))]

These contexts are commonly used in form views, list views, action domains, and relational field domains.

Understanding context in domains is your key to building smarter Odoo 19 applications. By using built-in variables like uid, company_id, and active_id, or creating custom keys like default_state, to automatically show exactly what they need. Think of context as current memory; it remembers who's logged in, which company they're working with, and what they're currently viewing. Whether you're passing context through actions, buttons, or wizards, mastering this flow gives you precise control over personalized user experiences.

To read more about The Ultimate Guide to Odoo 19 New Features and Enhancements, refer to our blog The Ultimate Guide to Odoo 19 New Features and Enhancements.


Frequently Asked Questions

Can I pass context from one module to another?

Yes, context flows between modules if you're passing it through the proper channels, actions, method calls, or field definitions. When you create an action that opens a view from your module, you can add context that will be available in the destination view.

What happens if I try to use a context key that doesn't exist?

If you reference a context key that doesn't exist in a domain, Odoo will treat it as None or False. This is why it's best practice to use fallback values when working with context in domains.

What are the most commonly used pre-available context variables?

uid - Current user's ID, company_id - Active company ID, allowed_company_ids - List of accessible companies, active_id - Currently selected record ID, active_ids - List of selected records, lang - User's language (e.g., 'en_US'), tz - User's timezone

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, KINFRA Techno Park
Kakkanchery, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message