KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have such a Book model: class Book(models.Model): authors = models.ManyToManyField(Author, ...) ... In short: I'd like to retrieve the books whose authors are strictly equal to a given set of authors. I'm not sure if there is a single query that does it, but any suggestions will be helpful. In long: Here is what I tried, (that failed to run getting an AttributeError) # 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)) final_books = QuerySet() for author in target_authors: temp_books = candidate_books.filter(authors__in=[author]) final_books = final_books and temp_books ... and here is what I got: AttributeError: 'NoneType' object has no attribute '_meta' In general, how should I query a model with the constraint that its ManyToMany field contains a set of given objects as in my case? ps: I found some relevant SO questions but couldn't get a clear answer. Any good pointer will be helpful as well. Thanks.
Tags (comma-separated)
Save Edits
Cancel