diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -1,6 +1,6 @@ from boards.models import Thread, Post from django.db import models -from django.db.models import Count +from django.db.models import Count, Sum __author__ = 'neko259' @@ -92,8 +92,12 @@ class Tag(models.Model): def get_post_count(self, archived=False): posts_count = 0 - for thread in self.threads.annotate(Count('replies')).filter( - archived=archived): - posts_count += thread.replies__count + threads = self.threads.filter(archived=archived) + if threads.exists(): + posts_count = threads.annotate(posts_count=Count('replies')).aggregate( + posts_sum=Sum('posts_count'))['posts_sum'] + + if not posts_count: + posts_count = 0 return posts_count