Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:51:27+00:00 2026-06-12T12:51:27+00:00

For the sake of simplicity let’s say I only have 2 models: Book, Author

  • 0

For the sake of simplicity let’s say I only have 2 models: Book, Author

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

class Book(models.Model):
    name = models.CharField(max_length='100')
    authors = models.ManyToManyField(Author)
    ...

I want to filter Books using a list of authors. What I tried to do is:

authors = [...] # a list of author objects
Books.objects.filter(authors__in=authors)

But here, the authors will be ORed when I want them ANDed.
Is there any way to AND many-to-many filtering??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:51:29+00:00Added an answer on June 12, 2026 at 12:51 pm

    You could & together a bunch of Q objects:

    q = Q()
    for author in authors:
        q &= Q(authors=author)
    Books.objects.filter(q)
    

    To exclude books that have authors outside the list, you could limit the query to books with exactly the number of authors in the list:

    Books.objects.annotate(count=Count('authors')).filter(count=len(authors)).filter(q)
    

    Update:

    Based on comments, I think the requirement is to get all books authored by at least one author in the list, but exclude books with any authors outside the list.

    So we build a queryset selecting the authors that we hate:

    # this queryset will be embedded as a subquery in the next
    bad_authors = Author.objects.exclude(name__in=['A1', 'A2'])
    

    Then exclude them to find the books we want:

    # get all books without any of the bad_authors
    Books.objects.exclude(authors__in=bad_authors)
    

    This will return all books except those authored by someone outside your list. If you also want to exclude those with no authors listed, add another exclude call:

    Books.objects.exclude(authors__in=bad_authors).exclude(authors=None)
    

    This will leave us with just the books authored by one or more of the good ones!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the sake of simplicity, let's say I have a Person class in Python.
For the sake of simplicity, let's say that we have input strings with this
I have 2 arrays, for the sake of simplicity let's say the original one
For simplicity's sake, let's say we have this rather contrived table: [ID] [Weekday] [Weather]
let's say (for the sake of simplicity) that I have and event that when
I have a dilemma. Let's assume(for simplicity's sake) I have four tables, with different
For the sake of simplicity, let's say my database has two tables, videos and
Lets say you have simple template function (not class member for the sake of
Let's say I have a class that implements the IDisposable interface. Something like this:
First some background. I have a file, for simplicity's sake, let's call it test.txt.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.