odoo14 | complex writing of domain in odoo

This article mainly talks about the logical conception of complex business requirements in domain, and the use of domainin will be reflected in another article.

· Binary tree and Polish

Before talking about the use of domain, let’s explain the content of binary tree traversal and Polish style in "Data Structure and Algorithm", which is a must for major students. If you know binary tree preorder traversal and Polish style conversion, please skip to the next section.

Binary tree: Binary tree (Binary tree) is an important type of tree structure. The data structure abstracted from many practical problems is often in the form of binary tree, and the structure of a certain binary tree is shown below.

 Basic terms related to trees:

In the binary tree shown above, there is a root node (F)

Then there is its left subtree (cadb) and right subtree (ehgm)

The subtree root node (B) of the left subtree (cadb) is a child node of the binary tree root node (F)

In the data structure, there are three ways to traverse the tree:

  1. preorder traversal
  2. Inorder traversal
  3. post order traversal

Pre-order traversal: it is the traversal of the left and right structures of the root, and the traversal order of the binary tree shown above is (FCADBEHGM)

In-order traversal: it is left-root-right structure traversal, and the traversal order is (ACBDFHEMG)

Post-order traversal: it is the traversal of the left and right root structures, and the traversal order is (ABDCHMGEF)

For the convenience of memory, the order of the roots can be used to correspond to the memory. The first access to the root node is the pre-order traversal

·Infix expressions, Polish and Reverse Polish

infix expression

Generally, the mathematical calculation formula we used in elementary school is the infix expression, that is, the form of 2*(1+3)

Converting it to a binary tree looks like this

 Infix expressions help us construct and understand binary trees, but for computers, this will be complicated and difficult to understand.

Therefore, Polish mathematical scientists proposed two traversal expressions suitable for computers. In order to commemorate their contributions, they were named Polish expressions and reverse Polish expressions.

The mathematics of Polish expressions and reverse Polish expressions are consistent with the traversal ideas of pre-order traversal and post-order traversal

The Polish expression is a concrete expression of the pre-order traversal of the binary tree. It first visits the root node, then visits the left child node, and finally visits the right child node. The result after traversal is *2+13

The reverse Polish expression is a concrete expression of the post-order traversal of the binary tree, that is, to visit the left child node first, then the right child node, and finally the root node. The result after traversal is 213+*

· Conversion of infix expressions and Polish

In Odoo’s domain, corresponding to the complex logic in the business, it is a waste of time for us to directly draw the binary tree and then traverse and access to write the Polish form. In fact, we can directly convert the infix expression to the Polish form

Still use case 2*(1+3) to analyze. In real business, each number represents a filter condition such as: ('state','=','pending'), and the operator represents a logical symbol such as:' |', so in the following analysis, numbers will be called business nodes, and transport symbols will be called logical nodes. As for the logical symbols that may appear in the domain and the corresponding levels, they will be displayed in a table, and the idea is first analyzed here.

Pre-order traversal: first separate the business node and the logical node with brackets (2)*((1)+(3)), then move the logical node forward by one level, and move the logical node at the same level as the business node to the last Left *(2)+((1)(3)), and then open all the brackets to get the result*2+13

The same is true for post-order traversal: (2)*((1)+(3)) >>> (2)((1)(3)+)* >>> 213+*

· domain in odoo

For the wording of domain in odoo [('a','=','b')] it happens to be more suitable to use Polish

Simple monotonic AND logic is used by default in the domain, for example: I want the state to be draft and pending and active to be true

[('state','in',('draft','pending')),('active','=',True)]

But for complex logic, you need to use Polish form

For example: I want to have data with department_id 2 and state draft or state pending and send_id 5

Expressed by infix expression, it is ((department_id=2)&(state=draft))|((state=pending)&(send_id=5))

Written into the domain is

['|','&',('department_id','=',2),('state','=','draft'),'&',('state','=','pending'),('send_id','=',5)]

おすすめ

転載: blog.csdn.net/weixin_45325204/article/details/127974680