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.