Question

I'd like to group records in two categories:

  1. Items having three or more records
  2. Items having less than three items

How do I go about this? I'm looking at using annotate().

Was it helpful?

Solution

q = Book.objects.annotate(num_authors=Count('authors'))
books_with_3_or_over_authors = q.filter(num_authors__gte=3)
books_with_less_than_3_authors = q.filter(num_authors__lt=3)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top