You are on page 1of 8

Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

odootricks.tips

Context in Odoo
7-9 minutes

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

Controlling how the data is displayed


Filters

Group By

Sorting

Displaying inactive records

Allowing force delete

Record Selection

Set language

Passing a default value

For example, if you create a contact from the Customers menu


option, it will be a customer. That only happens because Context is

1 of 8 2/23/2022, 4:27 AM
Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

used in the Windows Action:

{'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

Customer

Remember that partners include customers and suppliers


(vendors), and it makes sense that if you create a partner from
here it will be a customer.

In Odoo 12 and earlier, a partner could only be selected on a


Quotation / Sales Order if it was set as a customer. You can
manually select whether any partner is a customer, but having a
default is useful.

From Odoo 13 onwards the field is called customer ranking. It is


not displayed in the user interface, and any partner can be
selected on a Quotation / Sales Order, but customers are
displayed first.

Is a company

Partners can either be a company or an individual (which would be


linked to a company). So you might have the company Walmart
and individual buyers.

is_company means that you will create a partner company by


default.

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.

The same syntax can be used to set defaults within Form

2 of 8 2/23/2022, 4:27 AM
Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

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}" />

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.

Note: Record Rules and domain can also restrict what records are

3 of 8 2/23/2022, 4:27 AM
Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

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.

4 of 8 2/23/2022, 4:27 AM
Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

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:

5 of 8 2/23/2022, 4:27 AM
Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

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}"/>

6 of 8 2/23/2022, 4:27 AM
Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

Allow force delete

It is possible to override the Odoo validation by adding


'force_delete:True but this is not recommended!

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:

7 of 8 2/23/2022, 4:27 AM
Context in Odoo about:reader?url=https%3A%2F%2Fodootricks.tips%2Fabout%2Fbuildi...

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 of 8 2/23/2022, 4:27 AM

You might also like