Count records having three or more items
I'd like to group records in two categories:
- Items having three or more records
- Items 开发者_如何学Pythonhaving less than three items
How do I go about this? I'm looking at using annotate().
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)
精彩评论