I'm reading the beginnings of annotating and aggregating and I'm wondering which way is best to complete the following situation:
You want to count the number of authors for each book in a queryset. The tutorial suggests the following with annotate
:
Book.objects.annotate(Count('authors'))
My question is, why do it this way when you can accomplish this with model instance methods:
#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 %}
Is one situation better than the other? Are there optimization benefits I'm missing?