计算包含三个或更多项目的记录
-
12-09-2019 - |
题
我想将记录分为两类:
- 具有三个或更多记录的项目
- 少于三个项目的项目
我该怎么办?我正在考虑使用 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)
不隶属于 StackOverflow