diff gpp/polls/models.py @ 440:ac9217eef610

Added total vote information to the poll templates. Cache the total votes in the model instance.
author Brian Neal <bgneal@gmail.com>
date Sat, 21 May 2011 20:35:02 +0000
parents 1f139de929c4
children
line wrap: on
line diff
--- a/gpp/polls/models.py	Sat May 21 19:55:48 2011 +0000
+++ b/gpp/polls/models.py	Sat May 21 20:35:02 2011 +0000
@@ -68,12 +68,16 @@
 
         return (total_votes, choices)
 
-    def total_votes(self):
+    def _total_votes(self):
         """
         Returns the number of votes cast in this poll to date.
 
         """
-        return sum(choice.votes for choice in self.choice_set.all())
+        if not hasattr(self, '_total_votes_cache'):
+            self._total_votes_cache = sum(choice.votes for choice in
+                                            self.choice_set.all())
+        return self._total_votes_cache
+    total_votes = property(_total_votes)
 
     def is_open(self):
         now = datetime.datetime.now()