How to filter datas in OpenERP using domain list
Asked Answered
L

2

13

I want to filter recods in OPenERP using domain filter expression

In the recored I have a field of list of users, so i want get the record where the user logged in the list

[(user.id , 'in' , 'user_ids')]

This doesn't work

it return this error :

File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 2356, in search
    return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
  File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 4846, in _search
    self._apply_ir_rules(cr, user, query, 'read', context=context)
  File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 4728, in _apply_ir_rules
    rule_where_clause, rule_where_clause_params, rule_tables = rule_obj.domain_get(cr, uid, self._name, mode, context=context)
  File "/usr/lib/pymodules/python2.7/openerp/addons/base/ir/ir_rule.py", line 156, in domain_get
    query = self.pool.get(model_name)._where_calc(cr, SUPERUSER_ID, dom, active_test=False)
  File "/usr/lib/pymodules/python2.7/openerp/osv/orm.py", line 4676, in _where_calc
    e = expression.expression(cr, user, domain, self, context)
  File "/usr/lib/pymodules/python2.7/openerp/osv/expression.py", line 632, in __init__
    self.parse(cr, uid, context=context)
  File "/usr/lib/pymodules/python2.7/openerp/osv/expression.py", line 759, in parse
    field_path = left.split('.', 1)
AttributeError: 'int' object has no attribute 'split'

Please help me.

Lorelle answered 30/1, 2013 at 16:24 Comment(1)
How can we achieve this [#17723419Balaton
B
33

Your domain syntax is wrong.

It should be [('user_ids', '=' , user.id)]

  1. Each tuple in the search domain needs to have 3 elements, in the form: ('field_name', 'operator', value), where:

  2. field_name must be a valid name of field of the object model, possibly following many-to-one relationships using dot-notation, e.g 'street' or 'partner_id.country' are valid values.

  3. operator must be a string with a valid comparison operator from this list: =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right The semantics of most of these operators are obvious. The child_of operator will look for records who are children or grand-children of a given record, according to the semantics of this model (i.e following the relationship field named by self._parent_name, by default parent_id.

  4. value must be a valid value to compare with the values of field_name, depending on its type.

Domain criteria can be combined using 3 logical operators than can be added between tuples: '&' (logical AND, default), '|' (logical OR), '!' (logical NOT). These are prefix operators and the arity of the '&' and '|' operator is 2, while the arity of the '!' is just 1. Be very careful about this when you combine them the first time.

Here is an example of searching for Partners named ABC from Belgium and Germany whose language is not english ::

[('name','=','ABC'),'!',('language.code','=','en_US'),'|',('country_id.code','=','be'),('country_id.code','=','de')]

The '&' is omitted as it is the default, and of course we could have used '!=' for the language, but what this domain really represents is::

(name is 'ABC' AND (language is NOT english) AND (country is Belgium OR Germany))
Bethina answered 31/1, 2013 at 4:39 Comment(6)
Excellent. Such a detailed explanation is missing from the docs.Sessler
Great answer! Can I ask how you figure things out in Odoo while documentation is poor? What are your go to resources? ThanksLambda
I used to go into ORM files, checking all the classes and methods and the code.Bethina
Hi @SudhirArya, I am needing a domaing based on two conditions: 1. either product should exist in "product_ids" 2. or it should exist in "suggested_product_ids" with ('is_psa','!=',True), where *_ids fields are m2m and 'is_psa' a boolean Field. I have tried domain below with no luck. Thanks in advance. domain="['|',('id','in',product_ids[0][2])('is_psa','!=',True),('id','in','suggested_product_ids[0][2]'))]"Briefless
You should try this: domain="['|',('id', 'in', product_ids[0][2]), ('id', 'in', 'sugge‌​sted_product_ids[0][2]'), ('is_psa', '!=', True))]"Bethina
@SudhirArya can you help me to apply to domain on invoice field <field name="sup_refund_entries" nolabel="1" domain="['|',('period_id','=',period_ids),(,'&amp;',('date_invoice','&gt;',date_from),('date_invoice','&lt;',date_to)),'&amp;',('type','=','in_refund'),('state','=','open')] " > but its not working well can u tell me what is the issue ?Touristy
G
0

In a simple case this is right, BUT if you want to filter current object by its functional field, you'll be very surprised that code in function of this field will not execute; instead fnct_search part of this field will be executed, that will allow you to do various things.

Left part of filter expression is evaluated in context of current object, and right part — in context of inner context (read current user).

Left part is evaluated after right, so you can add a functional field to a user model, make some calculations there, and then receive those calculations on the object's side and take them into account.

See this answer for details: https://mcmap.net/q/903747/-how-to-use-functional-field-in-record-rule

Giuseppe answered 21/3, 2015 at 2:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.