6

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?

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193

1 Answers1

9

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
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193