Odoo 16 Development Book

Context & Domain

Using XML or Python, you can pass any information in context. Additionally, the domain is any requirement that aids in data filtering during searches. Therefore, when coding, context and domain will be more helpful. A context is a python dictionary that assists in passing necessary info to an Odoo function. You can see that practically every method in Odoo had a context argument to transmit data. Examples of many ways to use context are given below.

1. To pass default values for fields:

<field name="work_location_id" context="{'default_address_id': address_id}" />

This way, you can set the default value for a field.

2. Setting default filters and groups by records

<group expand="0" string="Group By">
            <filter string="Department" name="department" domain="[]" context="{'group_by': 'department_id'}"/>
            <filter string="Company" name="company" domain="[]" context="{'group_by': 'company_id'}" groups="base.group_multi_company"/>
            <filter string="Employment Type" name="employment_type" domain="[]" context="{'group_by': 'contract_type_id'}"/>
         </group>

Inside the context you can add the group_by field.

3. In Window actions

<record id="crm_lead_action_my_activities" model="ir.actions.act_window">
            <field name="name">My Activities</field>
            <field name="res_model">crm.lead</field>
            <field name="view_mode">tree,kanban,graph,pivot,calendar,form,activity</field>
            <field name="view_id" ref="crm_lead_view_list_activities"/>
            <field name="domain">[('activity_ids','!=',False)]</field>
            <field name="search_view_id" ref="crm.view_crm_case_my_activities_filter"/>
            <field name="context">{'default_type': 'opportunity',
                    'search_default_assigned_to_me': 1}
            </field>
            <field name="help" type="html">
                <p class="o_view_nocontent_smiling_face">
                    Looks like nothing is planned.
                </p><p>
                    Schedule activities to keep track of everything you have to do.
                </p>
            </field>
         </record>
         

For defining default values for new records, use context in window actions. The default type in this instance is set to opportunity.

4. Python has a function

Context is also usable inside of Python functions. You can access any value from XML passed to your Python function as self.partner id is the value that is passed from the XML in this statement: env.context.get('partner id').

5. In search view and filters

<group expand="0" string="Group By">
            <filter string="User" name="user" domain="[]" context="{'group_by':'user_id'}"/>
            <filter string="Type" name="type" domain="[]" context="{'group_by':'resource_type'}"/>
            <filter string="Company" name="company" domain="[]" context="{'group_by':'company_id'}" groups="base.group_multi_company"/>
            <filter string="Working Time" name="working_period" domain="[]" context="{'group_by':'calendar_id'}"/>
        </group>

Here added two filters inside the group tag, and passed the group_by using context.

Domain:

Domains can be used to search for or filter data or records depending on particular criteria. A field, operator, and value are all parts of a domain. You can filter the data using a variety of operators.

The syntax for the domain is:domain="[(field_name, 'operator', ‘value’)]"

The name of the field in the associated model to which the domain should be assigned is the field name. There are several distinct kinds of operators in Odoo.

-> comparison operators : < ,>, =, !=, <=,>=

-> [('name', '=like', ‘Mitchel’)] - This returns ‘Mitchel’

-> [('name', '=like', 'odoo')] - This returns ‘%odoo%’

-> [('name', '=like', 'odoo')] - This returns ‘%Odoo%’, ‘%odoo’

-> In’ and ‘not in’ : These are used to check if the value is present or not in the value list

-> child_of’ operator: It is used to find the child values in relation.

You can use domains in different situations

1. In a search view filter

<search string="Search for mrp workcenter">
            <field name="name" string="Work Center" filter_domain="['|', ('name', 'ilike', self), ('code', 'ilike', self)]"/>
            <filter name="archived" string="Archived" domain="[('active', '=', False)]"/>
            <group expand="0" string="Group By...">
                <filter string="Company" name="company" domain="[]" context="{'group_by': 'company_id'}" groups="base.group_multi_company"/>
            </group>
         </search>

2. In record rule

<record model="ir.rule" id="res_users_log_rule">
            <field name="name">res.users.log per user</field>
            <field name="model_id" ref="model_res_users_log"/>
            <field name="domain_force">[('create_uid','=', user.id)]</field>
            <field name="perm_read" eval="False"/>
         </record>

3. fields_view_get() method

Use the domain in the fields view get method to specify dynamic values for domain filters ().

4. To filter relational object fields records

partner_id = fields.Many2one('res.partner', 'Account Holder', ondelete='cascade', index=True, domain=['|', ('is_company', '=', True), ('parent_id', '=', False)], required=True)

5.To display specific records

<record id="action_bank_statement_tree" model="ir.actions.act_window">
            <field name="name">Bank Statements</field>
            <field name="res_model">account.bank.statement</field>
            <field name="view_mode">tree,pivot,graph</field>
            <field name="domain">['|', ('journal_id', '=', False), ('journal_id.type', '=', 'bank')]</field>
            <field name="context">{'journal_type':'bank'}</field>
            <field name="search_view_id" ref="view_bank_statement_search"/>
            <field name="help" type="html">
              <p class="o_view_nocontent_smiling_face">
                Register a bank statement
              </p><p>
                A bank statement is a summary of all financial transactions
                occurring over a given period of time on a bank account. You
                should receive this periodically from your bank.
              </p><p>
                Odoo allows you to reconcile a statement line directly with
                the related sale or purchase invoices.
              </p>
            </field>
         </record>
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message