If you’ve worked with Odoo for a while, you’ve probably seen the word “context” everywhere… but never fully understood what’s really happening behind the scenes.
The truth is:
Context is one of the most powerful features in Odoo.
It quietly passes data between:
- Actions
- Views
- Wizards
- Buttons
- Models
- One2many forms
And once you understand it properly, your development becomes cleaner, smarter, and much faster.
So… What Exactly is Context?
In simple terms:
Context is just a Python dictionary that carries extra information.
Example:
{
'default_partner_id': 7,
'search_default_customer': 1,
'active_id': 45,
'active_model': 'sale.order'
}
Think of it like a hidden messenger that tells Odoo:
- “Hey, pre-fill this value”
- “Apply this filter”
- “This is the record the user clicked”
- “Behave differently in this situation”
Where Does Context Actually Work?
Almost everywhere.
Context flows through:
- Window actions (ir.actions.act_window)
- Buttons & smart buttons
- Wizards
- One2many popups
- Server actions
Basically, whenever something opens, changes, or triggers — context is involved.
The Most Common Use Case: Default Values
This is where most developers first notice context.
Example: Passing data to a wizard
def action_open_wizard(self):
return {
'type': 'ir.actions.act_window',
'name': 'Create Followup',
'res_model': 'followup.wizard',
'view_mode': 'form',
'target': 'new',
'context': {
'default_partner_id': self.partner_id.id,
'default_sale_id': self.id,
}
}
Why default_?
Because Odoo does this automatically:
👉 default_partner_id → partner_id
So you don’t need extra code. It just works.
The Magic Trio: active_id, active_ids, active_model
Whenever you open something from a record, Odoo automatically passes:
{
'active_id': 12,
'active_ids': [12],
'active_model': 'sale.order'
}
This is extremely useful.
Example:
@api.model
def default_get(self, fields):
res = super().default_get(fields)
if self.env.context.get('active_id'):
sale = self.env['sale.order'].browse(self.env.context['active_id'])
res['partner_id'] = sale.partner_id.id
return res
Now your wizard knows where it came from.
You Can Even Use Context in XML
Not everything needs Python.
Smart button example:
<button name="action_open_invoice"
type="action"
context="{'default_partner_id': partner_id}"/>
One2many example:
<field name="order_line"
context="{'default_order_id': id}"/>
👉 Result: fields get auto-filled without extra logic.
Auto-Applying Filters (Very Useful!)
Context can activate filters automatically.
<field name="context">
{'search_default_customer': 1}
</field>
If a filter exists:
<filter name="customer"
domain="[('customer_rank', '>', 0)]"/>
👉 That filter is applied by default.
Changing Behavior Using Context
You can also control logic dynamically.
self.with_context(skip_validation=True).action_confirm()
Then inside your method:
if self.env.context.get('skip_validation'):
return
👉 Clean way to tweak behavior without breaking core logic.
Important Tip: Don’t Break Existing Context
This is a common mistake.
❌ Wrong:
'context': {'default_partner_id': self.partner_id.id}
✔ Correct:
'context': dict(self.env.context, default_partner_id=self.partner_id.id)
Always merge, don’t overwrite.
When Should You Use Context?
Use it when:
- Opening wizards
- Passing data between models
- Setting default values
- Applying filters
- Temporarily changing behavior
Avoid it when:
- Storing permanent data
- Passing large datasets
- Replacing actual fields
Real-Life Example (You’ve Already Used This!)
When you create an invoice from a Sales Order:
Odoo already uses context to pass:
- Customer
- Company
- Currency
- Fiscal position
That’s why everything is auto-filled.
Context might feel confusing at first…
But once you “get it”, everything clicks.
You’ll start to:
- Write less code
- Build smarter features
- Create better user experiences
Have an Odoo Requirement? Let's Talk.
Whether you're planning a new Odoo implementation, struggling with performance issues, or looking to build custom integrations — we're here to help.
Our team works with businesses of all sizes to design, develop, and optimize Odoo solutions that actually fit the way you work. From queue job architecture to full-scale ERP customization, we've got you covered.
📩 Reach out to us at: contact@opturatech.com 🌐 Whatsapp us at: +91 7025199191.
Or simply fill out our Contact Form → and we'll get back to you within one business day.