Context in Odoo 17: A Developer's Complete Guide

November 4, 2025 by
Context in Odoo 17: A Developer's Complete Guide
Jasson

In Odoo 17 development, the concept of context is a powerful tool that enables flexible behaviour without modifying the data model itself. Whether you’re setting default values for new records, passing parameters to wizards, or influencing how list/search views behave, using context effectively makes your modules more maintainable, reusable and dynamic. In this post, we’ll explore what context is, how it’s used in Odoo 17 (both in the server/ORM side and the view side), walk through practical examples, highlight best practices and common pitfalls, and show how you can leverage context to deliver smarter customisations.


What is Context in Odoo?


At its simplest, context in Odoo is a Python dictionary (key-value pairs) used to carry metadata or parameters through the system. The context is available via env.context on the server side, and is also passed through actions and views on the client side.

Key aspects of context:

  • It allows you to pass default values for fields (via keys like default_fieldname).
  • It enables default filters or groupings in views (via keys like search_default_…, group_by).
  • It can be used in buttons/actions to pass parameters
  • On the Python side you can read self.env.context.get(...) 
  • It supports the "immutable" practice, rather than changing the env.context directly, you typically user .with_context() on a recordset to carry additional context.
  • In Odoo 17 especially, as the web client evolves (OWL, etc), context handling remains critical to connecting front-end action parameters to backend logic


Where Context is Used


1. Default Values for New Records

When you open a "new record" form (via an action or a button), you often want certain field pre-filled. You can use context for this. For example:

<record id="action_create_my_model" model="ir.actions.act_window">
  <field name="name">New MyModel</field>
  <field name="res_model">my.module.model</field>
  <field name="view_mode">form</field>
  <field name="context">{'default_user_id': user.id, 'default_state': 'draft'}</field>
</record>

With this, wen the form opens, user_id is defaulted to current user, state defaults to 'draft', this approach avoids adding custom "default" logic into your create methods.


2. Default Filters & Grouping in Search Views / Actions

You can influence list/search views via context to set default filters or groupings. For example:

  • Use search search_default_fieldname:1 in the action context to enable a filter by default
  • Use group_by: 'fieldname'  in filter's context inside a <group> section in a search view
<search>
   …
   <group expand="0" string="Group By">
     <filter string="Company" name="company" context="{'group_by':'company_id'}"/>
   </group>
</search>

These allow your menus/actions to pre-filter or pre-group records as required by your business logic.


3. Passing Parameters to Wizards / Buttons / Actions

When you trigger an action (via a button, menu, or server method), you can embed context in the action to pass values to the form that opens. Example:

<button name="%(action_open_wizard)d"
        type="action"
        context="{'default_partner_id': partner_id}"/>

Then the action (ir.actions.act_window) might have:

<field name="context">{'default_partner_id': context.get('partner_id')}</field>

This pattern lets you pass values from current record to the wizard, opening the wizard with fields pre-filled.


4. In Python / ORM Side

On the server/model side you receive the context via self.env.context. You can inspect it, for example:

@api.model
def create(self, vals):
if self.env.context.get('default_user_id'):
vals['user_id'] = self.env.context['default_user_id']

return super().create(vals)

This allows you to adapt logic based on what values were passed via context


How to Use Context in Odoo 17 


XML / View Side

<record id="action_new_employee" model="ir.actions.act_window">
  <field name="name">New Employee</field>
  <field name="res_model">hr.employee</field>
  <field name="view_mode">form</field>
  <field name="context">{'default_company_id': company_id, 'default_state': 'draft'}</field>
</record>


Default Filter and Group in Search View

<record id="view_search_my_model" model="ir.ui.view">
  <field name="name">my.model.search</field>
  <field name="model">my.module.model</field>
  <field name="arch" type="xml">
    <search>
      <field name="name"/>
      <filter string="Active" name="active" domain="[('active','=',True)]"/>
      <group expand="0" string="Group By">
        <filter string="Category" name="category" context="{'group_by':'category_id'}"/>
      </group>
    </search>
  </field>
</record>

<record id="action_open_my_model" model="ir.actions.act_window">
  <field name="name">My Models</field>
  <field name="res_model">my.module.model</field>
  <field name="view_mode">tree,form</field>
  <field name="context">{'search_default_active':1}</field>
</record>


Python Side

class MyModel(models.Model):
    _name = 'my.module.model'

    user_id = fields.Many2one('res.users', string='User')

    @api.model
    def create(self, vals):
        # override default user if context provides one
        if self.env.context.get('default_user_id'):
            vals['user_id'] = self.env.context['default_user_id']
        return super(MyModel, self).create(vals)

    def some_method(self):
        # call another method but pass context
        return self.with_context(my_custom_param=True)._other_method()


Why Context Matters for Odoo Developers


In Odoo, the platform continues evolving with improved UI, OWL components, modular architecture and more complexity in business processes. Context remains one of the simplest yet most effective mechanisms to:

  • Adapt behaviour dynamically without heavy customisation.
  • Design reusable modules where behaviour is controlled by passed data rather than hard-coding
  • Bridge UI and backend logic
  • Deliver better user-experience: default values, filters, groups all contribute to smoother workflows.


Conclusion


Mastering context in Odoo 17 unlocks a higher level of development skill, you’ll build cleaner, more flexible modules; provide better UX with default values and filters; and ensure your logic stays maintainable. While the concept is straightforward—a dictionary of parameters—the art lies in using it appropriately, naming keys clearly, and integrating context both in the XML/view side and Python/server side. As you work more with Odoo 17, you’ll find yourself relying on context not just for “toy” defaults, but for enabling sophisticated workflows and user-driven behaviours.

Take the time to review your current custom modules: search for .with_context(), check your action window definitions and search view filters, and see where context might already be in use (or where it should be). With the practices above, you can elevate your Odoo development to the next level.

Context in Odoo 17: A Developer's Complete Guide
Jasson November 4, 2025
Share this post
Archive