In Odoo 19, context is one of the main actors to control the data flow between views, fields, actions, and backend methods. This serves as a temporary dictionary to pass extra information through the system. When creating or navigating to a record, developers frequently use context to pass dynamic data between components, to set default values, and to control behaviour.
Chained context is an advanced usage of this mechanism where values are passed step-by-step across multiple fields or actions in a step. Chained context provides a continuous flow of information between related fields and models, not just a single value transfer. It reduces manual data entry, improves user experience, and maintains records that are automatically linked throughout the entire workflow.
What is Context in Odoo?
Context in Odoo is a Python dict that is used to pass additional information to XML views, Python methods, and actions. This enables Odoo to know how a given operation should behave under certain conditions.
Example:
{'default_partner_id': 10, 'lang': 'en_US'}Context is commonly used to:
- Set default values
- Pass flags between methods
- Control field behaviour dynamically
- Filter or prefill records
- Transfer data between views and actions
For example, if you open a form view from a customer record, the customer information can be automatically passed through context, so you don’t need to type the same data over and over.
What is Chained Context?
The chained context is an improvement of the normal context mechanism in Odoo. Data flows through several layers, where each layer is dependent on the previous one rather than just passing values once.
The flow will usually look like this:
Field A > context passes > Field B > context passes > Field C
This technique is especially useful when working with related models such as Customers, Sales Orders, and Order Lines.
1. Basic Chained Context in XML
One of the simplest examples of chained context is passing values from one field to another inside an XML form view.
<field name="partner_id"/>
<field name="order_id"
context="{'default_partner_id': partner_id}"/>
In this example:
- The user selects a customer in partner_id
- When opening the order_id field pop-up, Odoo automatically passes the selected customer
- The Sales Order form receives the following:
default_partner_id = selected partner_id
This automatically fills the customer field in the newly created order.
2. Multi-Level Chained Context
Context can also be passed across multiple dependent fields.
<field name="country_id"/>
<field name="state_id"
context="{'default_country_id': country_id}"/>
<field name="city_id"
context="{'default_state_id': state_id}"/>
Here, the data flow works step by step:
- Country is selected
- The selected country is passed to State
- The selected state is then passed to City
This type of chaining is useful when building hierarchical forms with dependent records.
3. Chained Context in Actions
Context can also be transferred while opening a new window action.
<button name="action_open_orders"
type="object"
context="{'default_partner_id': partner_id}"/>
The same behaviour can be implemented through Python actions.
def action_open_orders(self):
return {
'type': 'ir.actions.act_window',
'name': 'Orders',
'res_model': 'sale.order',
'view_mode': 'list,form',
'context': {
'default_partner_id': self.partner_id.id,
}
}
In both cases, the selected customer is automatically passed to the Sales Order form.
4. Using Chained Context in Python
Context values passed through chained actions can be accessed in Python using self.env.context. This is useful when implementing conditional business logic based on the source of the action or additional parameters passed through the context.
def action_confirm(self):
source = self.env.context.get('source_screen')
if source == 'crm':
pass
This allows developers to pass information across multiple views and actions while maintaining a clean and flexible workflow.
5. Chained Context with Default Values
Odoo provides a predefined pattern called:
default_<field_name>
This pattern automatically fills fields during record creation.
Example:
context="{'default_user_id': uid}"When the form opens, Odoo automatically assigns the current user to the user_id field.
Some commonly used default context keys include:
- default_partner_id
- default_company_id
- default_user_id
- default_order_id
Using these defaults simplifies form handling and reduces manual entry.
6. Real-Time Chaining Using Onchange
In some cases, developers combine onchange methods with context to pass values dynamically.
@api.onchange('partner_id')
def _onchange_partner(self):
return {
'context': {
'default_partner_id': self.partner_id.id
}
}This updates the context whenever the customer field changes and helps maintain consistent data flow across related fields.
7. Conditional Chained Context
Context values can also be controlled conditionally.
<field name="order_id"
context="{
'default_partner_id': partner_id if partner_id else False
}"/>
Here:
- If a customer is selected, the value is passed
- Otherwise, the context remains empty
This avoids unnecessary or invalid data transfer.
8. Context Chaining in One2many and Many2many Fields
Chained context is widely used in One2many and Many2many relationships.
<field name="order_line_ids"
context="{
'default_order_id': id,
'default_partner_id': partner_id
}"/>
When adding a new line:
- order_id is filled automatically
- partner_id is also passed to the line
This is commonly used in Sales Orders, Purchase Orders, and Invoice Lines.
Important Rules About Context
Chained context is powerful, but there are some important behaviours for developers to know:
- The context is non-reactive
- Does not automatically refresh after the field is updated
Context is only evaluated if:
- Open a view
- Clicking on a field
- Initiating an action
- Insert a new record
Thus, developers combine context with onchange methods for dynamic behaviour.
Real-World Example of Full Context Chaining
A common real-world scenario involves Customers, Sales Orders, and Order Lines.
<field name="partner_id"/>
<field name="order_id"
context="{'default_partner_id': partner_id}"/>
<field name="order_line_ids"
context="{
'default_order_id': id,
'default_partner_id': partner_id
}"/>
Workflow:
- The user selects a Customer
- The Sales Order automatically receives the customer value
- While adding Order Lines:
- Customer information is also passed automatically
This creates a smooth workflow with minimal manual input.
Chained context is a useful way to pass values between related fields, actions, and models in Odoo 19. It helps developers develop cleaner workflows and reduces the need to enter data repeatedly by passing information from one step to another. Chained context works in XML views, Python methods, actions, relational fields, and all over the system to improve form behaviour and keep records connected. To build efficient and user-friendly Odoo apps, you need to understand how the context travels through the different parts.
To read more about A Complete Guide to Mastering Context in Odoo 19 for Developers, refer to our blog A Complete Guide to Mastering Context in Odoo 19 for Developers.