1
나는 annotating and aggregating의 시작을 읽고 있어요 나는 다음과 같은 상황을 완료하기 위해 어떤 방법을 가장 궁금 해요 :간단한 장고 집계 - 최적화
당신이 검색어 세트의 각 책에 대한 저자의 수를 계산합니다.
Book.objects.annotate(Count('authors'))
내 질문은, 왜 모델 인스턴스 방법으로이 작업을 수행 할 수는이 방법을 수행 할 때 :
#models.py
class Book(models.Model):
authors = models.ManyToManyField(Author)
def author_count(self):
return self.authors.count()
#views.py
books = Book.objects.all()
#template.html
{% for book in books %}
{{ book.author_count }}
{% endfor %}
이 다른 쪽보다 상황이 더 나은가요 튜토리얼은 annotate
와 함께 다음과 같은 제안? 누락 된 최적화 혜택이 있습니까?