Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

6

complex many2many domains in views

By
YannickB
on 1/17/14, 5:12 AM 7,565 views

Hello everyone,

I have some complex many2many domain to do and I am not able to make it works. Let me described the case :

I created two many2many fields in res.partner, skill_category_ids and skill_tags_ids

They point to two object marketplace.category and marketplace.tag. The object marketplace.category has only the name field. The object marketplace.tag has the name field and a required many2one field which point to marketplace.category.

In res.partner form, I want to be able to only select the tags which belong to category we selected in skill_category_ids. Here is my try :

<field name="skill_category_ids"/>
<field name="skill_tag_ids" domain="[('category_id','in',skill_category_ids)]"/>

It give the following traceback when I try to open the skill_tags_ids field :

  Server Traceback (most recent call last):
  File "/opt/openerp/openerp-wezer-dev/web/addons/web/session.py", line 89, in send
    return openerp.netsvc.dispatch_rpc(service_name, method, args)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/netsvc.py", line 292, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/service/web_services.py", line 626, in dispatch
    res = fn(db, uid, *params)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/osv/osv.py", line 188, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "/opt/openerp/openerp-wezer-dev/server/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/osv/orm.py", line 2369, in search
    return self._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/osv/orm.py", line 4887, in _search
    cr.execute('SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str, where_clause_params)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/sql_db.py", line 161, in wrapper
    return f(self, *args, **kwargs)
  File "/opt/openerp/openerp-wezer-dev/server/openerp/sql_db.py", line 226, in execute
    res = self._obj.execute(query, params)
TypeError: not all arguments converted during string formatting

And this is not all, I also created an object marketplace.announcement which have a many2one to marketplace.category and a many2many to marketplace.tags. I want to create a filter which will only display announcement which have their category in the skill_category_ids of the user or have at least one tag in the skill_tags_ids of the user.

Here is my try :

<filter name="my_skills" string="My skills" domain="['|',('category_id','in',uid.partner_id.skill_category_ids),('tag_ids','in',uid.partner_id.skill_tag_ids)]" icon="terp-check"/>

I have the following error message when I load the filter :

TypeError: results.group_by is undefined

This is really a complex case, with domain between two many2many fields, I don't think I'll be able to figure out by myself how I'll achieve that, and I didn't found a workaround until now. I hope you guys will have some ideas.

Thanks you, Yannick.

12

Olivier Dony (odo)

--Olivier Dony (odo)--
2112
| 7 6 7
--Olivier Dony (odo)--

 

Olivier Dony (odo)
On 1/17/14, 6:10 AM

For the first part of the question, you have to know that the web client outputs the value of local "many2many" using the writeable syntax. So if you have 3 entries in a m2m it will return them in this format: [(6,0,[ID1,ID2,ID3])]. This means that the domain [('category_id','in',skill_category_ids)] will actually render as: [('category_id','in',[(6,0,[ID1,ID2,ID3])])] which is invalid and will give the kind of error you are seeing. You can easily see it if you look at the actual RPC being triggered in your web browser (network debug).

A quick and dirty fix for this is to change your domain expression to extract the list of IDs from the value returned by the client: [('category_id','in',skill_category_ids and skill_category_ids[0][2])]. Another way would be to set an on_change trigger on the skill_category_ids field and set the domain for the relevant fields in the result of the on_change.


For the second part, your filter domain is invalid because it has to be evaluated on the client-side only (it's dynamically applied when you use the search view). In this context the uid value is just a pure integer, not a browse_record (this concepts exists only on the server-side). You have to write the domain in a different way that only requires uid on the right side, for example (relationship names may be wrong, I only guessed them):

<filter name="my_skills" string="My skills" domain="['|',('category_id.tag_ids.partner_ids.user_ids','in',uid),('tag_ids.partner_ids.user_ids','in',uid)]" icon="terp-check"/>

One of the best answers on help.oerp!

Thanks a lot!

The only thing that confuses me, is that the 'in' operator in domains seems to be 'reversible'... ([list], in, int) oO

René Schuster
on 1/17/14, 7:33 AM

@René: yes this may be surprising at first, the meaning of the in operator is reversed when used for a one2many or many2many field. It is basically a contains operator in that case, but OpenERP does not have this operator. It may be easier to understand if you think of it as a comparison that is attempted for each existing line of the relationship. For example [('x2m_ids', '=', 12)] is valid and will match any record that has line 12 in its x2m_ids, regardless of the other lines. Domains onx2many fields are often non-trivial because of this (especially negative expressions!)

Olivier Dony (odo)
on 1/17/14, 7:47 AM

wow. thx. i've been looking for this explanation for a very long time now.

René Schuster
on 1/17/14, 8:38 AM

As competent as ever Olivier, really thanks a lot this is a really good answer. Now it works perfectly, mainly I didn't though that many2many was also a 6,0,[ids] in view, and for second part I needed to create the one2many tag_ids and partner_ids to use your code, which is now done. Thanks again :).

YannickB
on 1/17/14, 9:54 AM

I can report that [0][2] works in Odoo 9. Saying it because I thought it doesn't: tried about 100 times, then it started working.

Aron Lorinc
on 12/30/15, 2:08 AM

I have a problem, my field is m2m and I like to filter if it has more them one rows, like this domain="[[u'tag_ids', u'&gt', 1]], but did not work and I don't have any ideia of how to make a function to count the number of rows, could you give some help?

Cezar Jr
on 2/5/16, 5:34 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 1/17/14, 5:12 AM
Seen: 7565 times
Last updated: 10/19/16, 7:37 AM