Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding OpenERP Domain Filter?

Tags:

odoo

I would like to ask you if you could please explain the anatomy of the Openerp domain filters. I have to use it my project. Please explain the description of the following domain filter.

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

I want to know the exact meaning of (order_id.user_id','=',user.id), what is order_id, user_id, and user.id. Are they referencing any table. If yes then how am I supposed to know which one...

Basically I want to know decipher the notation from bottom up so that can use it as per my requirement.

like image 211
manish.engineer Avatar asked Sep 25 '13 07:09

manish.engineer


2 Answers

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!!

like image 121
Vivek Avatar answered Sep 23 '22 22:09

Vivek


The '|' is an OR that gets applied to the next comparison. The (..., '=', False) gets converted into an IS NULL so the SQL for this would be

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

The default is AND which is why you don't see ('&', ('field1', '=' ,1), ('field2' ,'=', 2) everywhere.

Note that another useful one is ('field1', '!=', False) which gets converted to WHERE field1 IS NOT NULL

There isn't a lot of great documentation for this and they get quite tricky with multiple operators as you have to work through the tuples in reverse consuming the operators. I find I use complex ones infrequently enough that I just turn on query logging in Postgres and use trial and error observing the generated queries until I get it right.

like image 40
Adrian Merrall Avatar answered Sep 25 '22 22:09

Adrian Merrall