Domains in Odoo are used to search, filter, or limit records based on
specific conditions. A domain is structured as a list of tuples,
where each tuple typically contains a field name, an operator, and a
value. This flexible system supports a wide range of operators,
allowing for complex and dynamic filtering of records based on
various criteria.
The domain syntax is expressed as:
domain="[(field_name, 'operator', 'value')]".
The field name in a domain refers to the specific field within the
model to which the condition is applied. Odoo supports a variety of
domain operators, each designed to perform a distinct type of
comparison or evaluation, enabling precise and flexible filtering of
records.
- 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
You can leverage the fields_view_get method to dynamically define
domain filters at runtime. By modifying the domain within this
method, you can tailor the filtering logic based on user context,
record values, or other dynamic conditions, allowing for more
flexible and intelligent behavior in views.
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 ['|',
('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.