# HG changeset patch # User neko259 # Date 2014-11-20 16:43:30 # Node ID 72a583330abfe0b79035f83806f7c421ded6f794 # Parent dec155c19dd28be7f38034b71abaf4391d20872e Use aggregation to get the thread images count diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -1,5 +1,5 @@ import logging -from django.db.models import Count +from django.db.models import Count, Sum from django.utils import timezone from django.core.cache import cache from django.db import models @@ -81,12 +81,8 @@ class Thread(models.Model): return self.replies.count() def get_images_count(self): - # TODO Use sum - total_count = 0 - for post_with_image in self.replies.annotate(images_count=Count( - 'images')): - total_count += post_with_image.images_count - return total_count + return self.replies.annotate(images_count=Count( + 'images')).aggregate(Sum('images_count'))['images_count__sum'] def can_bump(self): """