Context in Odoo

Context is used to control what is displayed in:

This is either:

  1. Defined in a Windows Action associated with a menu option OR
  2. Passed from one view to another OR
  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 but users can change this default and create an “individual” contact (linked to a company).
 –  Note that either can be selected on a quotation / sales order.

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_id" context="{'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_id" context="{'default_partner_id':partner_invoice_id,
'default_name':name}
" />

Create analytic account for sales order

Note that these are defaults, so users can make changes (though sometimes the field may be read-only or invisible, in which case they can’t).

Filters

Context can be used to set a Filter when displaying records from a table.

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

A filter will further limit what is displayed, but the user can remove it.

For example, the “Customers” menu initially displays a list of customers, and not any other type of contact. There is no domain specified for this menu option but there is a default filter set using Context (in the slightly confusingly named “Filters” section on the Windows Action):

{'search_default_customer': 1}

Note: the customer filter is a defined in the Search View. If you need to add a new filter you will have to update the search view.

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

If users remove the filter it will display suppliers as well (this might be useful if a contact has been setup as a supplier and should also be a customer) but of course the display is still limited by domain and Record Rules.

The same syntax can be used in Form Views to filter lists:

<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 a supplier or other contact to be selected.

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']} 

Expand

'expand': 1

Expand a grouped list

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)
</tree>

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>
</xpath>

OR

<page string="Order Lines" name="order_lines">
  <field name="order_line"
    widget="section_and_note_one2many"
    mode="tree,kanban"
    context="{'lang': 'fr_FR'}">

8 thoughts on “Context in Odoo

  1. Can it be used to display additional information in many2one, when selecting?
    e.g., I have custom model, which has name and id.
    I add this field onto a Contact form. When searched, it’s only possible to search by name, not id.

    Maybe you can guide me into a direction on how to see and search by both (name and id)?

    Like

    1. I don’t think this can be done by using context, but take a look at products: you can search by the product Name or the Internal Reference

      Like

  2. how can I pass a value in view to the context
    like this

    type=”object” name=”open_related_model”
    context=”{‘target_state’:value}”>

    and tried this also

    type=”object” name=”open_related_model” t-att-context=”{‘got_state’: value}”

    didn’t work for both

    Like

  3. Would it be possible through the Record Selection to select only Shipping Addresses from the selected customer’s commercial partner?

    Example for SO
    Customer: Deco Addict, Addison Olson
    Then Shipping Address field would only show the Delivery Addresses for Deco Addict.

    I added the [,’commercial_partner_id’:partner_id.commercial_partner_id] part to this line:

    But this just results in an error when trying to select the Shipping Address. Is it possible to fetch related fields or am I just doing it wrong?

    Like

  4. For the example context of {‘group_by’: [‘date_invoice:month’, ‘user_id’]}
    Is it possible to pass this in as a search default? This way, it will appear as a token in the search bar.

    Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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