문제

레코드를 두 가지 범주로 그룹화하고 싶습니다.

  1. 3 개 이상의 레코드가있는 항목
  2. 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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top