The 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[1] business logic in the case of the 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 record set to get the desired results from a method call or the desired value for a computed 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
In this example, it set a default value for company_id
2. Using context for setting default filters and groups by records.
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 the context in window actions is given below.
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
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 the python function which is passed in an 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.
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 the domain.
Operator: The list of operators used are given below
*Usual comparison operators: < , >, =, !=, <=, >=
*=like’ operator: This operator matches against a pattern. The ‘%’ symbol matches 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.
2. Using domain in record rule.
Setting domain force in the following example in case employee_id.user_id is user.id
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
Reference:
ORM(Object Relational Mapping) in Odoo is a concept or technique which acts as a bridge between your programming language and your database. Object Relational Mapping helps to execute SQL queries without writing them explicitly. Once the ORM is configured in an application, the user can use the OOP concepts like classes and objects to interact with the database. Read more about ORM (Object Relational Mapping) in Odoo
Odoo Blogs Odoo Development Tutorials Odoo 13 Book