Globalteckz

Using Context and Domain in Odoo

  • Home
  • Using Context and Domain in Odoo
HOW TO USE CONTEXT AND DOMAIN IN ODOO

Using Context and Domain in Odoo

How to use context and domain in Odoo ERP | Context in Odoo is a part of the environment of a record set. It is basically a python dictionary that carries the session data. This data can be used on both the client-side user interface and also the ORM business logic in case of server-side. So most importantly we can use the context to pass the specific data to a function in odoo. You may have seen that almost all methods in Odoo use a context parameter. Context is used to pass information such as the time zone or the language of the user interface as well as the contextual parameters specified in the actions in odoo.

What is context in Odoo ERP software : 

Context is a dictionary that carries information such as user ID, user language, Timezone, etc. by default. With that one can pass any kind of data or information in context as per the requirements and needs or through xml or from python (methods) files and then based on that extra information can be written to process code.

Let discuss how to use context in Odoo:

A number of methods in the Odoo standard add on’s use the context data to adapt the behavior to these values. It is sometimes necessary to modify the context on a record set to get the desired results from a method call or the desired value for a compute field. You can pass any kind of data or information using context as per your need or requirement either from the XML file or from the python files (method files). So based on that later you can write the process code using the information that you got from the context that you have given.

  1. Using the Context to pass default values for fields

How to Use Context and Domain in Odoo - Using the Context to pass default values for fields

In the above, it set a default value for company_id

  1. Using context for setting default filters and groups by records.

How to Use Context and Domain in Odoo - context for setting default filters

  1. Using Context in Window actions

We know window actions are an important part of odoo architecture. Context can be used in window actions to set default values for new records or to display inactive records. An example of context in window actions is given below.

How to Use Context and Domain in Odoo -Context in Window actions

  1. Using context to pass in default values for relational field objects

Both the context and domain are particularly useful in relational fields. Default values for related records can be defined using context

How to Use Context and Domain in Odoo -context to pass in default values for relational field objects

In the above example it set a default value for company_id in a many2one field company_id.

  1. Fetching value from XML by using context in python function.

You can access the value in python function which is passed in XML file using context as follows.

Suppose you have passed a value partner_id in your XML file, in your python function you can access it as self.env.context.get(‘partner_id’). self.env.context.get() function is used to check whether that key is present in your context. So make sure that your context contains this parameter and also it contains a value.

  1. Using context in search view and filters.

How to Use Context and Domain in Odoo context in search view and filters.

Inside the group tag, we have added two filters and passed the group_by in the context

What is Domain in Odoo:

Domain is a condition which we used to filter the data or for searching. A domain expression consist of list of conditions. Each condition is a (‘field_name’, ‘operator’, ‘value’) tuple which has been described below

  • field_name : The field name (in which you want to set domain.)
  • value : The value is evaluated as a Python expression. It can use literal values, such as numbers, booleans, strings, or lists, and can use fields and identifiers available in the evaluation context.
  • operator :

The usual comparison operators are < , > , <= , >= , = , != .

      • ‘=like’ matches against a pattern, where the underscore symbol, _ , matches any single character, and the percentage symbol, % , matches any sequence of characters.
      • ‘like’ matches against a ‘%value%’ pattern. The ‘ilike’ is similar but case insensitive.
      • The ‘not like’ and ‘not ilike’ operators are also available.
      • ‘child of’ finds the children values in a hierarchical relation, for the models configured to support them.
      • ‘in’ and ‘not in’ are used to check for inclusion in a given list, so the value should be a list of values. When it is to be used When it is to be used in “to-many” relation field the ‘in’ operator behaves like a CONTAINS operator.

Let discuss how to use Domain in Odoo:

The syntax is as follows:

domain=”[(field_name, ‘operator’, ‘value’)]”

field_name: In this, the field name indicates simply the field name in which you want to assign domain.

Operator: The list of operators used are given below

*Usual comparison operators: < , >, =, !=, <=, >=

*=like’ operator: This operator matches against a pattern. The ‘%’ symbol matches against any sequence of characters. It returns the exact case sensitive search.

Eg: [(‘name’, ‘=like’, ‘odoo’)] – this returns ‘odoo’

*like’ operator: This operator matches against a ‘%value%’ pattern, which is similar to ‘ilike’ operator. It also returns a case sensitive search.

Eg: [(‘name’, ‘=like’, ‘odoo’)] – this returns ‘%odoo%’.

*ilike’ operator:  This operator returns exact case insensitive search.

Eg: [(‘name’, ‘=like’, ‘odoo’)] – this returns ‘%Odoo%’, ‘%odoo’

*not like’ and ‘not ilike’ operators: ‘not like’ operator returns result not matched with case sensitive and ‘not ilike’ operator returns result not matched with case insensitive.

*In’ and ‘not in’ operator: These operators are used to check whether the value is present or not present in a list of values.

*child_of’ operator: This operator is used to find children’s values in a hierarchical relation.

Some of the examples of a domain are given below.

  1. Using domain in a search view filter.

How to Use Context and Domain in Odoo domain in a search view filter.

2. Using domain in record rule.

Setting domain force in the following example in case employee_id.user_id is user.id

How to Use Context and Domain in Odoo Using domain in record rule.

3. Using domain in fields_view_get() method to set dynamic values for domain filters.

4. A domain can also be used to filter relational object fields records with conditions.

5. Using the domain to display specific records based on condition

How to Use Context and Domain in Odoo - domain to display specific records based on condition

To Summarize : Therefore, domain is a filter you cannot expand beyond – context is a filter you can expand to the point that domain allows. The above screenshot of the tutorial how to use context and domain in odoo has been taken considering Odoo version 13 for latest version of Odoo ERP somehow the process remains the same. For any further technical questions feel free to contact us on sales@globalteckz.com

About Globalteckz:

At Globalteckz we offer our clients with world class Business and Enterprise solutions. We are presence in India, USA, &  Canada, and have successfully offered end to end solutions to our clients. Some of our services offering include leading Open Source Solutions such as Odoo, Magento, and others ecommerce development. We deliver excellence and quality for Information Technology & Software Solutions to our clients worldwide.

For questions on Odoo pricing and cost that can involve with your business you can contact our sales team on sales@globalteckz.com

Read more articles related to Odoo Pricing :

Leave Comment

×