Mercurial > public > sg101
comparison gpp/downloads/models.py @ 192:341759e1cda1
Implementing #67: use a denormalized count field on download categories to reduce database queries.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 03 Apr 2010 01:10:00 +0000 |
parents | 0e4961833cdf |
children | b4305e18d3af |
comparison
equal
deleted
inserted
replaced
191:046e6ef0ff45 | 192:341759e1cda1 |
---|---|
13 | 13 |
14 class Category(models.Model): | 14 class Category(models.Model): |
15 """Downloads belong to categories.""" | 15 """Downloads belong to categories.""" |
16 title = models.CharField(max_length=64) | 16 title = models.CharField(max_length=64) |
17 description = models.TextField(blank=True) | 17 description = models.TextField(blank=True) |
18 count = models.IntegerField(default=0, blank=True) | |
18 | 19 |
19 class Meta: | 20 class Meta: |
20 verbose_name_plural = 'Categories' | 21 verbose_name_plural = 'Categories' |
21 ordering = ('title', ) | 22 ordering = ('title', ) |
22 | 23 |
23 def __unicode__(self): | 24 def __unicode__(self): |
24 return self.title | 25 return self.title |
25 | |
26 def num_downloads(self): | |
27 return Download.public_objects.filter(category=self.pk).count() | |
28 | 26 |
29 | 27 |
30 def download_path(instance, filename): | 28 def download_path(instance, filename): |
31 """ | 29 """ |
32 Creates a path for a download. Uses the current date to avoid filename | 30 Creates a path for a download. Uses the current date to avoid filename |
112 download = models.ForeignKey(Download) | 110 download = models.ForeignKey(Download) |
113 user = models.ForeignKey(User) | 111 user = models.ForeignKey(User) |
114 vote_date = models.DateTimeField(auto_now_add=True) | 112 vote_date = models.DateTimeField(auto_now_add=True) |
115 | 113 |
116 def __unicode__(self): | 114 def __unicode__(self): |
117 return "%s voted on '%s' on %s" % ( | 115 return u"%s voted on '%s' on %s" % ( |
118 self.user.username, | 116 self.user.username, |
119 self.download.title, | 117 self.download.title, |
120 self.vote_date.strftime('%b %d, %Y %H:%M:%S')) | 118 self.vote_date.strftime('%b %d, %Y %H:%M:%S')) |
121 | 119 |
122 class Meta: | 120 class Meta: |