Development Book V17: Domain

Domains enable searching or filtering data based on specific criteria. A domain consists of a field, operator, and value, allowing diverse filtering options using various operators.

The domain syntax is expressed as:

domain="[(field_name, 'operator', 'value')]"

The field name, representing the associated model where the domain should be applied, is referred to as the field name. Odoo employs various operators to perform distinct operations.

  • Comparison operators: <, >, =, !=, <=, >=
  • Example: ['name', '=like', 'Mitchel'] returns records with the name 'Mitchel'.
  • Example: ['name', '=like', 'odoo'] returns records with the name containing 'odoo'.
  • Example: ['name', '=ilike', 'odoo'] returns records with the name containing 'Odoo' or 'odoo'.
  • 'In' and 'not in': Used to check the presence or absence of a value in a list.
  • 'child_of' operator: Identifies child values in a relation.

Domains are versatile and applicable in various scenarios.

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

Utilize the domain in the fields_view_get method to define 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>

In the provided Odoo XML code, the < field name="domain">['|', ('journal_id', '=', False), ('journal_id.type', '=', 'bank')] line is responsible for defining a domain filter. The domain filter is used to specify conditions that records must meet to be included in the view.

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