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:
- ลูกค้าสัมพันธ์
- e-Commerce
- ระบบบัญชี
- สินค้าคงคลัง
- PoS
- Project
- MRP
คำถามนี้ถูกตั้งค่าสถานะ
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".
สนุกกับการพูดคุยนี้ใช่ไหม? เข้าร่วมเลย!
สร้างบัญชีวันนี้เพื่อเพลิดเพลินไปกับฟีเจอร์พิเศษและมีส่วนร่วมกับคอมมูนิตี้ที่ยอดเยี่ยมของเรา!
ลงชื่อRelated Posts | ตอบกลับ | มุมมอง | กิจกรรม | |
---|---|---|---|---|
|
0
ก.พ. 24
|
3627 | ||
|
0
พ.ค. 25
|
66 | ||
|
2
มี.ค. 25
|
2472 | ||
|
2
ม.ค. 25
|
6013 | ||
computed fields
แก้ไขแล้ว
|
|
1
เม.ย. 24
|
2062 |
Domain in many2many field: https://learnopenerp.blogspot.com/2018/12/add-domain-on-many2many-field-in-odoo.html