odoo开发:domain表达式这样更好理解

odoo开发

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

OperatorInstructions
=,>,=,Comparison operations, equal to, not equal to, greater than, greater than or equal to, less than, less than or equal to
likeFuzzy matching, match by %value%
=likeYou can use pattern matching: underscore _ matches a single character, percent sign % matches zero or more characters.
ilikeSimilar to like, but case-insensitive
=ilikeSimilar to =like, but case-insensitive
not likeThrough %value% mismatched
not ilikeSimilar 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 =.
inDetermine whether the value is in the element's list
not inDetermine whether the value is not in the list of elements
child_ofDetermine 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


SymbolInstructions
&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>

关于我们

​我们致力于帮助中小企业实现数字化转型,我们的团队由一群充满激情和创新思维的专业人士组成,他们具备丰富的行业经验和技术专长。

扫一扫获取顾问以及手册

归档
登录 留下评论
odoo13【安排的动作】功能如何实现自动化运行相关动作