세 개 이상의 항목이있는 레코드를 계산하십시오
-
12-09-2019 - |
문제
레코드를 두 가지 범주로 그룹화하고 싶습니다.
- 3 개 이상의 레코드가있는 항목
- 3 개 미만의 항목이있는 항목
이것에 대해 어떻게 가나 요? 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