I am trying to filter out records based on their tags, many2many field. I know that I can filter records using notation with name, as in:
[('tag_ids','=','TAGNAME')]
or with tag id, as in:
[('tag_ids.id','=',123)]
What I would like to do is filter OUT records that contain specific tag, say 'OUT'. Suprisingly this works as I want it to work:
[('tag_ids','!=','OUT')]
However, this presents a problem, because tag.name is language supported field, and I cannot list all possible translations in filter. I would rather use syntax with specific tag id, say:
[('tag_ids.id','!=',123)]
But this does not work. It works, if the only tag on the record is 123, but if there is any other tag on the object, it does not filter the record out.
So, is there any syntax to use so that domain filter would filter out records with given tag, so that tag is specified simply with id?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Contabilità
- Magazzino
- PoS
- Project
- MRP
La domanda è stata contrassegnata
Hi Roy:
That's because when you have multiple tags, the id of each tag gets compared when you use [('tag_ids.id','!=',123)]. So the record gets selected if it contains any tag that is not 123.
Try this instead.
["!", ("tag_ids","in",[123])]
Hi, and thanks, this works. Great!
However, I still don't understand why. The principle I understand, but how odoo decides what field to use in comparison.
E.g. this uses name field: [('tag_ids','=','TAGNAME')]
Whereas this uses id: ("tag_ids","in",[123])
I would understand in better if we would always use explicit field definition, like 'tag_ids.id', but here seems to be some heuristic I do not understand, and is not documented anywhere? Is it related to the operator ('=' vs. 'in'), or is it determined by value type or what?
One2many and Many2many fields like tag_ids in this case, return the recordset. When used in a comparison without a specific field, they reference the name field defined for the model. A good example of seeing this in action is in the Export functionality - if you export the field tag_ids you will get the name field but if you specifically select one of the fields the value of that field will get exported.
Thanks for the heuristic explanation, Paresh. But here ("tag_ids","in",[123]), Odoo is still using the recordest without any specific field, and is still correctly using the field "id" instead of the "name".
Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!
Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!
RegistratiPost correlati | Risposte | Visualizzazioni | Attività | |
---|---|---|---|---|
|
0
feb 24
|
3626 | ||
|
0
mag 25
|
55 | ||
|
2
mar 25
|
2469 | ||
|
2
gen 25
|
6012 | ||
computed fields
Risolto
|
|
1
apr 24
|
2057 |
Domain in many2many field: https://learnopenerp.blogspot.com/2018/12/add-domain-on-many2many-field-in-odoo.html