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 6326101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:01:30+00:00 2026-05-24T17:01:30+00:00

I am working on a Django app for which I have encountered a tricky

  • 0

I am working on a Django app for which I have encountered a tricky aggregate query that I would like to evaluate.

In the demo app of my project, I declared the following model classes for representing libraries’ holdings:

from django.db import models

class Book(models.Model):
    interesting = models.BooleanField()

class Library(models.Model):
    books = models.ManyToManyField(Book)

What I would like to query is the maximum number of “interesting” books in a single library, which is the maximum of the counts of books at each library that are “interesting”.

In SQL, this is:

select max(a.num_interesting_books) as max
from (select count(demo_book.id) as num_interesting_books
      from demo_book
      inner join demo_library_books on (demo_book.id = demo_library_books.book_id)
      where demo_book.interesting=TRUE
      group by demo_library_books.library_id) as a

 

Using the following test data:

insert into demo_library(id) values (1), (2), (3);
insert into demo_book(id, interesting) values
(1, FALSE), (2, FALSE), (3, TRUE),
(4, TRUE), (5, TRUE),
(6, TRUE), (7, TRUE), (8, TRUE), (9, FALSE);
insert into demo_library_books(library_id, book_id) values
(1, 1), (1, 2), (1, 3),
(2, 4), (2, 5),
(3, 6), (3, 7), (3, 8), (3, 9), (3, 3);

the above SELECT statement results in:

 max
-----
   4
(1 row)

as expected.

Is it possible to use Django’s querying API to calculate this value?

  • 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-24T17:01:31+00:00Added an answer on May 24, 2026 at 5:01 pm

    I think I figured it out:

    Library.objects.filter(books__interesting=True).annotate(num_interesting_books=Count('pk')).aggregate(max=Max('num_interesting_books'))
    

    Translated to SQL, this is:

    select max(a.num_interesting_books) as max
    from (select demo_library.*, count(demo_library.id) as num_interesting_books
          from demo_library
          inner join demo_library_books on (demo_library.id = demo_library_books.library_id)
          inner join demo_book on (demo_library_books.book_id = demo_book.id)
          where demo_book.interesting=TRUE
          group by demo_library.id) as a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a Django app and I have a class which reads
I'm working on a Google App Engine project using Django. I noticed that for
I am working through the examples in a Django book that I have, but
I am working on a web application using Python (Django) and would like to
I'm working on a large Django app, the vast majority of which requires a
A Django app that I am working has an Event model. An Event may
I have a Django project that I need mounted at two different subdirectories of
I'm working on a django app right and I'm using cherrypy as the server.
Hey, i am currently working on a django app for my studies, and came
I'm working on a little django app for reserving prints of paintings. Customers go

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.