Django: ManyToMany filter matching on ALL items in a list
Similar to @goliney’s approach, I found a solution. However, I think the efficiency could be improved. # A sample set of authors target_authors = set((author_1, author_2)) # To reduce the search space, first retrieve those books with just 2 authors. candidate_books = Book.objects.annotate(c=Count(‘authors’)).filter(c=len(target_authors)) # In each iteration, we filter out those books which don’t contain … Read more