我找到了一个解决方案。但是,我认为效率可以提高。
# A sample set of authorstarget_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 one of the # required authors - the instance on the iteration.for author in target_authors: candidate_books = candidate_books.filter(authors=author)final_books = candidate_books


