7

我想问你是否可以解释一下 Openerp 域过滤器的结构。我必须在我的项目中使用它。请解释以下域过滤器的描述。

['|',('order_id.user_id','=',user.id),('order_id.user_id','=',False)]

我想知道(order_id.user_id','=',user.id)order_iduser_id和的确切含义user.id。他们是否引用任何表格。如果是,那我怎么知道是哪一个...

基本上我想知道从下到上破译符号,以便可以根据我的要求使用它。

4

2 回答 2

21

This one is pretty simple.

Consider the following fields (only XML i've given here, python you got to manage)

<field name="a"/>
<field name="b"/>
<field name="c"/>

Single Condition

Consider some simple conditions in programming

if a = 5  # where a is the variable and 5 is the value

In Open ERP domain filter it would be written this way

[('a','=',5)] # where a should be a field in the model and 5 will be the value

So the syntax we derive is

('field_name', 'operator', value)

Now let's try to apply another field in place of static value 5

[('a','=',b)] # where a and b should be the fields in the model

In the above you've to note that first variable a is enclosed with single quotes whereas the value b is not. The variable to be compared will be always first and will be enclosed with single quotes and the value will be just the field name. But if you want to compare variable a with the value 'b' you've to do the below

[('a','=','b')] # where only a is the field name and b is the value (field b's value will not be taken for comparison in this case)

Condition AND

In Programming

if a = 5 and b = 10

In Open ERP domain filter

[('a','=',5),('b','=',10)]

Note that if you don't specify any condition at the beginning and condition will be applied. If you want to replace static values you can simply remove the 5 and give the field name (strictly without quotes)

[('a','=',c),('b','=',c)]

Condition OR

In Programming

if a = 5 or b = 10

In Open ERP domain filter

['|',('a','=',5),('b','=',10)]

Note that the , indicates that it's and condition. If you want to replace fields you can simply remove the 5 and give the field name (strictly without quotes)

Multiple Conditions

In Programming

if a = 5 or (b != 10 and c = 12)

In Open ERP domain filter

['|',('a','=',5),('&',('b','!=',10),('c','=',12))]

Also this post from Arya will be greatly helpful to you. Cheers!!

于 2013-09-28T19:07:37.590 回答
3

'|' 是应用于下一个比较的 OR。(..., '=', False) 被转换成一个 IS NULL 所以这个 SQL 将是

WHERE order_id.user_id = x OR order_id.user_id is NULL

默认值为 AND,这就是为什么您在任何地方都看不到 ('&', ('field1', '=' ,1), ('field2' ,'=', 2) 的原因。

请注意,另一个有用的是 ('field1', '!=', False) 它被转换为 WHERE field1 IS NOT NULL

没有很多很好的文档,而且使用多个运算符会变得非常棘手,因为您必须通过元组反向使用运算符。我发现我很少使用复杂的查询,所以我只在 Postgres 中打开查询日志记录,并使用反复试验观察生成的查询,直到我做对为止。

于 2013-09-26T20:06:58.893 回答