more natural way to count total item in django
authorymh <ymh.work@gmail.com>
Tue, 23 May 2017 19:10:45 +0200
changeset 525 7f3fdcba7902
parent 524 a051b60f4b26
child 526 6c879e963a93
more natural way to count total item in django
src/iconolab/models.py
--- a/src/iconolab/models.py	Tue May 23 16:49:39 2017 +0200
+++ b/src/iconolab/models.py	Tue May 23 19:10:45 2017 +0200
@@ -54,12 +54,16 @@
     show_image_on_home = models.BooleanField(default=False)
 
     @cached_property
+    def items_count(self):
+        return self.items.count()
+
+    @cached_property
     def completed_percent(self):
 
         items_with_annotation = \
             ImageStats.objects.filter(image__item__collection = self, annotations_count__gt=0)\
                 .values('image__item').distinct().count()
-        total_items = Item.objects.filter(collection=self).count()
+        total_items = self.items_count
 
         return int(round((items_with_annotation * 100) / total_items))