开发者

Count records having three or more items

I'd like to group records in two categories:

  1. Items having three or more records
  2. 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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜