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

  • Home
  • SEARCH
  • 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 4060346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:19:35+00:00 2026-05-20T15:19:35+00:00

I have a query that requires to filter exactly 2 authors with the ID

  • 0

I have a query that requires to filter exactly 2 authors with the ID

Theoretically,

Book.objects.filter(author__id=1, author__id=2). 

which is not possible.

How can I solve this problem?

Cheers,
Mickey

  • 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-05-20T15:19:35+00:00Added an answer on May 20, 2026 at 3:19 pm

    Not intuitive at first but the answer is right in front of us.

    Book.objects.filter(author__id=1).filter(author__id=2)
    

    If you want an exact match, you could potentially further filter this result by those items that only have exactly 2 authors.

    Book.objects.annotate(count=Count('author')).filter(author__id=1)\
                    .filter(author__id=13).filter(count=2)
    

    If you want exact matches dynamically, how about something like this?:

    def get_exact_match(model_class, m2m_field, ids):
        query = model_class.objects.annotate(count=Count(m2m_field))\
                    .filter(count=len(ids))
        for _id in ids:
            query = query.filter(**{m2m_field: _id})
        return query
    
    matches = get_exact_match(MyModel, 'my_m2m_field', [1, 2, 3, 4])
    
    # matches is still an unevaluated queryset, so you could run more filters
    # without hitting the database.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq-to-Entities query that is not complicated but requires an .include and/or
I have a query that I know can be done using a subselect, but
I have an sql query that requires repeating the same SELECT statement a number
I have a table which has a couple of columns that do not have
I am using in C# MYsql .I have query that works if I run
I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
I have a query that updates one record, and only one record. Is there
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a query that might be executed by several users consecutively. I'm scared
I have a query that shows me a listing of ALL opportunities in one

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.