You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now I want a result, where every message has exact those two tags - not just one of them. I've tried it with __name__exact=_q, but it isn't working. Any ideas how to solve this issue?
The text was updated successfully, but these errors were encountered:
I haven't tried this but I believe that doing Message.objects.filter(tags__name="tag1").filter(tags__name="tag2") should give you what you want.
That should return your messages that include tag1.... that are then filtered down by messages including tag2 (so the end result has those including both).
If you want to find those with exactly those two tags (i.e. no other tags), then you will want to filter those down, then there's a bit more work involved.
has_both_tags = Message.objects.filter(tags__name="tag1").filter(tags__name="tag2")
# get all the tag objects that you are not interested in (using the through model)
all_other_tags = Message.tags.through.objects.exclude(name__in=["tag1", "tag2")
# filter out messages that have any of those other tags
only_both_tags = has_both_tags.exclude(tags__in=all_other_tags)
I haven't tested this but the through model should be helpful for you. Core idea, mathematically is:
get the set of messages with both tags
get all the tags that you do not care about
get the set of messages with any tags you don't care about
I'm trying to do this:
Now I want a result, where every message has exact those two tags - not just one of them. I've tried it with __name__exact=_q, but it isn't working. Any ideas how to solve this issue?
The text was updated successfully, but these errors were encountered: