##// END OF EJS Templates
Use aggregation to get tags popularity
neko259 -
r607:9b64cf8c default
parent child Browse files
Show More
@@ -1,6 +1,6 b''
1 from boards.models import Thread, Post
1 from boards.models import Thread, Post
2 from django.db import models
2 from django.db import models
3 from django.db.models import Count
3 from django.db.models import Count, Sum
4
4
5 __author__ = 'neko259'
5 __author__ = 'neko259'
6
6
@@ -92,8 +92,12 b' class Tag(models.Model):'
92 def get_post_count(self, archived=False):
92 def get_post_count(self, archived=False):
93 posts_count = 0
93 posts_count = 0
94
94
95 for thread in self.threads.annotate(Count('replies')).filter(
95 threads = self.threads.filter(archived=archived)
96 archived=archived):
96 if threads.exists():
97 posts_count += thread.replies__count
97 posts_count = threads.annotate(posts_count=Count('replies')).aggregate(
98 posts_sum=Sum('posts_count'))['posts_sum']
99
100 if not posts_count:
101 posts_count = 0
98
102
99 return posts_count
103 return posts_count
General Comments 0
You need to be logged in to leave comments. Login now