This question has been flagged
3 Replies
6076 Views

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?

Avatar
Discard
Best Answer

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])]


Avatar
Discard
Author

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.