Chapter 6 - Odoo 15 Development Book

Context and Domain

You can pass any information in context by using XML or python. And domain is any condition that helps to filter the data when searching. So Context and domain will help you more when coding.

Context:

Context is actually a python dictionary that helps to pass needful data to a function in odoo. In odoo, you can see almost all functions had the context parameter to pass data.

Examples for using context in different ways:

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="Status" name="status" domain="[]" context="{'group_by': 'state'}"/>
  <filter string="Company" name="company" domain="[]" context="{'group_by': 'company_id'}"
      groups="base.group_multi_company"/>
</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>

You can use context in window actions for setting default values for new records. In this example the default type is set as opportunity.

4. In python function

You can also use context inside a python function. If you pass any value from XML, then you can access it from your python function as self.env.context.get(‘partner_id’), Here partner_id is the value that passes from the XML.

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="Model" name="model" domain="[]" context="{'group_by':'model_id'}"/>
</group>

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

Domain:

You can use domains for searching or filtering data or records based on specific conditions. A domain contains a field, operator and value. You can use different operators for filtering the data.

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

field_name is the name of the field of the corresponding model to assign the domain. In case of operator there are different types 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 a 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)]"/>
</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

To set dynamic values for domain filters, you can use the domain in the method fields_view_get().

4. To filter relational object fields records

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

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,form,pivot,graph</field>
  <field name="domain">[('journal_id.type', '=', 'bank')]</field>
  <field name="context">{'journal_type':'bank'}</field>
  <field name="search_view_id" ref="view_bank_statement_search"/>
</record>
whatsapp_icon
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