0

There is ManyToMany field in Entry model called tags.
How do I filter Entries, which contain exact set of selected tags?

E.g. get all entries, that have tags with ids 1,2,3 and not 1,2,3,4 or 1,2
Something like this:

selected_tags = Tag.objects.filter(id__in=[id1,id2,id3])
entries = Entry.objects.filter(tags=selected_tags)

Thanks!

cezar
  • 11,616
  • 6
  • 48
  • 84
  • @petr thank you, it is what I was looking for. By the way, could you explain, why node_query = node_query.filter(tags__pk=pk) in the for loop and not node_query += node_query.filter(tags__pk=pk). Will it find only one Node with the given pks? – Maxim Kolodnikov Feb 06 '18 at 08:19
  • Sure. Each round will filter the entries with the particular tag. So after all rounds you are sure that all the entries have all the tags. – petr Feb 06 '18 at 10:14

0 Answers0