1

Let's say we have a many2many relationship as follows:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100) 
    authors = models.ManyToManyField(Author, related_name="books")

and now I have a known set of Authors:

authors = Author.objects.filter(...)

How can I get all the books that are authored by that exact set of authors?

books = Book.objects.filter(X)

what is X? Such that I have all the books authored by exactly that set of authors?

I have tried, just because:

books = Book.objects.filter(authors__in=authors)

But nah, that returns the books to which any of those authors contributed it seems. I want the books that all of those authors contributed to and only those authors.

Thus far, this has stumped me.

1 Answer 1

2

You can check if the number of matching authors is the amount of authors, so:

from django.db.models import Count, Q

nauthors = authors.count()

Book.objects.alias(
    nauthors=Count('authors'),
    nmatch_authors=Count('authors', filter=Q(authors__in=authors)),
).filter(
    nauthors=nauthors,
    nmatched_authors=nauthors
)

It is important that authors does not contain duplicates.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks immensely for this, rapid and functioning response. Got me rolling!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.