how-to-use-context-and-domain-in-odoo-13.jpg
Blogger_637079905001142766.png
By: Mehjabin Farsana P

How to Use Context and Domain in Odoo 13

Technical Odoo 13

Context in odoo is a part of the environment of a recordset. 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. A number of methods in the odoo standard addons use the context data to adapt the behavior to these values. It is sometimes necessary to modify the context on a recordset 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. 


So let’s see how to use context through the following examples.


1. Using the Context to pass default values for fields


how-to-use-context-and-domain-in-odoo-13


In this example, it set a default value for company_id


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


how-to-use-context-and-domain-in-odoo-13


3. 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-13


In the above example, the default payment type is set as ‘inbound’ using context.


4.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-13


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


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


6. Using context in search view and filters.


how-to-use-context-and-domain-in-odoo-13


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


Domain


Odoo domain is used to select records from a model or database table.  It is a very common use case when you need to display a subset of all available records from an action, or to allow only a subset of possible records to be the target of a many2one relation. The way to describe these filters in Odoo is called a domain. It can be defined as a list of operations that are used to filter your data or for searching. 


Each condition in a domain contains a field name, operator, and value.


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


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


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.


how-to-use-context-and-domain-in-odoo-13


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


how-to-use-context-and-domain-in-odoo-13



cybrosys youtube

Comments

0


Leave a comment

 
Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message