1. Domain expression rules
The simplest format: [('field name', 'operator', value)]
Example: [('shenqr.user_id','=',uid)]
Field name
If it is a Many2one/Many2Many/One2many field, you can retrieve the corresponding attribute using '.', for example: ('create_uid.company_id.parent_id','=', user.company_id.id)
Operator
| Operator | Instructions |
|---|---|
| =,>,=, | Comparison operations, equal to, not equal to, greater than, greater than or equal to, less than, less than or equal to |
| like | Fuzzy matching, match by %value% |
| =like | You can use pattern matching: underscore _ matches a single character, percent sign % matches zero or more characters. |
| ilike | Similar to like, but case-insensitive |
| =ilike | Similar to =like, but case-insensitive |
| not like | Through %value% mismatched |
| not ilike | Similar to not like, but case-insensitive |
| =? | Not set or equals. Not set means when the value is None or False; otherwise, it is the same as =. |
| in | Determine whether the value is in the element's list |
| not in | Determine whether the value is not in the list of elements |
| child_of | Determine whether it is a sub-record of value, [(A,'child_of',A)] returns true |
Mainly explain child_of:
[('create_uid.company_id','child_of',[user.company_id.id])]
Equivalent to
['|',('create_uid.company_id','=',[user.company_id.id]),('create_uid.company_id.parent_id','=',[user.company_id.id])]
value
Including strings, True or False, numbers, etc.
In XML, you can retrieve the current logged-in user object, and you can retrieve user.id, user.company_id.id, etc.
Logic between conditions
| Symbol | Instructions |
|---|---|
| & | And, indicates that two conditions after the symbol are satisfied simultaneously, default relationship, can be omitted |
| | | Or, it means that only one of the following two conditions needs to be met. |
| ! | No, reverse the following condition; if it is true, change it to false. |
Odoo uses Polish notation. Simply put, Polish notation is a method where operators are placed before operands, and expressions can still be parsed unambiguously without parentheses. #### Order of Operations in Polish Notation ####
Taking binary operations as an example, read the expression from left to right. When an operator is encountered followed by two operands, compute it, then replace the operator and the two operands with the result as an operand; repeat this step until all operators have been processed.
['|','&','|',a,b,c,'&',d,e]
Where a,b,c,e,f,g are expressions without logical operators, and the order of operations for the expressions:
['|','&','|',a,b,c,'&',d,e]
['|','&',(a | b),c,'&',d,e]
['|',((a | b) & c),'&',d,e]
['|',((a | b) & c),(d & e)]
[(((a | b) | c) | (d & e))]
Conversely, if we want to achieve this effect
A and (B or C) and D and E
Start from the inside, move "or" forward
A and (or B C) and D and E
Move the "and" inside forward and remove the parentheses.
and A or B C and D E
So the final domain can be written like this
A, '|', B, C, D, E
2. Domain usage scenarios
1. Related Fields
(Many2one/One2many/Many2many) Filter out different selection results
account_id = fields.Many2one('account.account', string='Default Debit Account', domain=[('active', '=', False)])
2. Filter out the fields you want to display
Add Domain in ir.actions.act_windon, so that when this action is called somewhere, only records that meet the domain conditions are displayed.
<record id="relate_partner_opportunities" model="ir.actions.act_window">
<field name="name">Opportunities</field>
<field name="res_model">crm.lead</field>
<field name="view_mode">kanban,tree,form,graph,calendar</field>
<field name="domain">[('type','=','opportunity')]</field>
<field name="context">{ 'search_default_partner_id': active_id, 'default_type': 'opportunity' }</field>
<field name="view_id" eval="False"/><field name="search_view_id" ref="crm.view_crm_case_opportunities_filter"/>
</record>
3. Permission Filtering
<record id="account_move_comp_rule" model="ir.rule">
<field name="name">Account Entry</field><field name="model_id" ref="model_account_move"/>
<field name="global" eval="True"/>
<field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field>
</record>
4. Search Filter
<record id="view_crossovered_budget_search" model="ir.ui.view">
<field name="name">crossovered.budget.search</field>
<field name="model">crossovered.budget</field>
<field name="arch" type="xml"><search string="Budget">
<field name="name" filter_domain="[('name','ilike',self)]" string="Budget"/>
<field name="date_from"/><field name="date_to"/><filter string="Draft" domain="[('state','=','draft')]" help="Draft Budgets"/>
<filter string="To Approve" domain="[('state','=','confirm')]" help="To Approve Budgets" />
<field name="state"/></search>
</field>
</record>
