콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
8186 화면

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?

아바타
취소
베스트 답변

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".

관련 게시물 답글 화면 활동
0
2월 24
4034
naked domain set up 해결 완료
3
7월 25
4316
0
5월 25
883
2
1월 25
6763
computed fields 해결 완료
1
4월 24
2731