Context in Odoo

Context is used to control what is displayed in:

  • Form Views
  • List / Tree Views
  • Kanban Views
  • User-defined Filters
  • Buttons (which is more technical, and not covered here) 

This is either:

  1. Defined in a Windows Action associated with a menu option
  2. Passed from one view to another.
  3. Used within a Form View to control how information is displayed

These are some of the things that can be done:

Passing a default value

If you create a contact from the Customers menu option, it will be a customer. That only happens because Context is used in the Windows Action: 

Windows Action Filters

{'default_customer':1,'default_is_company': True}

It will always start with  {“default_  then the field name and value.  In this case there are two fields:

  • customer = ‘1’ (True)    
  • is_company = True 

is_company means that you will create a partner company (rather than an individual).

Users can change this default and create an “individual” contact (linked to a company).

On a quotation / sales order, companies or individual customers can be selected.

In Odoo 12 and earlier you can also un-check ‘Is a Customer’ if you really want (in which case this partner could not be selected on a Quotation / Sales Order).

The same syntax can be used with Form Views.

For example, if you create a new tax in the Sales app it should be a sales tax:

<field name=taxes_id  context={‘default_type_tax_use’:’sale’}/>

As well as a constant we can use a variable value.  In the next example if you create an analytic account for a sales order it will be for the customer on the order:

<field name=analytic_account_idcontext={‘default_partner_id’:partner_invoice_id} />

Note that the 2nd parameter is a field name rather than a constant.

Of course, you can have defaults for multiple fields, and in this example we default both the the customer and the sales order number (again, it’s to create an Analytic Account):

<field name=analytic_account_idcontext={‘default_partner_id’:partner_invoice_id,

Create analytic account for sales order

Note that these are defaults, so normally you can change them (though sometimes the field may be read-only or invisible, in which case you can’t).


Context can be used to specify which Filter will be used when displaying records from a  table.

Note: Record Rules and domain can also restrict what records are displayed. Both of these are ‘hard’ restrictions that a user cannot bypass.

  • Filter will further limit what is displayed, but the user can remove the filter.  

For example, the “Customers” menu initially displays a list of customers, and not any other type of contact.

Note that there is no domain specified for this menu option (and the Context actually has more options, but let’s keep it simple to start). This is how we apply a default filter (in the Windows Action):

{'search_default_customer': 1}

This is the filter (note the ‘X’ to show that it can be removed):

If users remove the filter it will display all contacts (this might be useful if a contact has been setup as a supplier and should also be a customer).

The same syntax can be used in Form Views:

<field string=Customer name=partner_id context={‘search_default_customer‘:1} />

This means that the “Customer” filter will be applied (as above).

However, in most cases domain will be used because it doesn’t make sense to allow anything other than a customer to be selected – and filter without domain would allow that! 

Group By

Context can be used to group records in a List View (primarily for menus and user-defined filters). This is the syntax:

 {'group_by': ['date_invoice:month', 'user_id']} 

Sort order

This allows the initial sort order to be set for a List View or Kanban View

Context (in Windows Action)

{'show_sale': True, 'search_default_order_confirmed': 1}

XML syntax for sorting (for reference – does not use Context):

<tree default_order="date desc">
<field name="date"/>
(other fields)

Display inactive records

As standard, Odoo will only display active records from a table, but it’s possible to override this.

We can see this in the menu option for Currencies. This is the Windows Action for Accounting / Configuration / Currencies:

The important part is the Context Value:

{'active_test': False, 'search_default_active': 1}

Note that this also applies a filter (using search_default) so that initially only active currencies are displayed:

Filter ON

Remove the filter to also display inactive records

  • It’s easy to see which currencies are inactive because they appear after the active currencies and in a grey font:
Filter OFF and inactive currencies displayed

This is because Odoo comes with all currencies already setup and you should activate rather than create.

This technique can also be used in XML on a one2many-field (but it’s not common)

<field name="<field_name>" context="{'active_test': False}"/>

It’s used for currency selection, and also on the Product Template for a service item (if Project Management is installed):

<field name="project_template_id" context="{'active_test': False}"/> 

Record Selection

This is very similar to what can be done with domain.

For example, on the Contacts Form View, you only want to be able to choose states for the selected country (e.g. United States).  This is the syntax:

<field name="state_id" context="{'country_id': country_id}"/> 

This is the result:

However, it’s easier to use domain for this, and that is what you will usually find in Odoo.  

Set language

As standard, a user selects a language and Odoo should display everything in the chosen language (though you will have to check the translation and ensure that anything you add has been translated).

It’s also possible to select a language using context. This might be useful if a user primarily uses one language but also has to check information in other languages and doesn’t want to switch their language preference every time.

The syntax is as follows (using French as an example):

{'lang': 'fr_FR'}

In the XML, it will look something like this:

<xpath expr="//field[@name='order_line']" position="attributes">
<attribute name="context">{'lang': 'fr_FR'}</attribute>


<page string="Order Lines" name="order_lines">
<field name="order_line"
context="{'lang': 'fr_FR'}">

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s