comparison gpp/weblinks/models.py @ 193:fa7d82bfb100

Implement #68: add a denormalized count field to weblinks Category model to reduce database queries.
author Brian Neal <bgneal@gmail.com>
date Sat, 03 Apr 2010 02:15:04 +0000
parents 046e6ef0ff45
children b4305e18d3af
comparison
equal deleted inserted replaced
192:341759e1cda1 193:fa7d82bfb100
1 ''' 1 """
2 This module contains the models for the weblinks application. 2 This module contains the models for the weblinks application.
3 ''' 3 """
4
5 from django.db import models 4 from django.db import models
6 from django.contrib import auth 5 from django.contrib import auth
7 6
8 7
9 class Category(models.Model): 8 class Category(models.Model):
10 '''Links belong to categories''' 9 """Links belong to categories"""
11 title = models.CharField(max_length = 64) 10 title = models.CharField(max_length=64)
12 description = models.TextField(blank = True) 11 description = models.TextField(blank=True)
12 count = models.IntegerField(default=0)
13 13
14 def __unicode__(self): 14 def __unicode__(self):
15 return self.title 15 return self.title
16
17 def num_links(self):
18 return Link.public_objects.filter(category = self.pk).count()
19 16
20 class Meta: 17 class Meta:
21 verbose_name_plural = 'Categories' 18 verbose_name_plural = 'Categories'
22 ordering = ('title', ) 19 ordering = ('title', )
23 20
28 return super(PublicLinkManager, self).get_query_set().filter( 25 return super(PublicLinkManager, self).get_query_set().filter(
29 is_public=True).select_related() 26 is_public=True).select_related()
30 27
31 28
32 class Link(models.Model): 29 class Link(models.Model):
33 '''Model to represent a web link''' 30 """Model to represent a web link"""
34 category = models.ForeignKey(Category) 31 category = models.ForeignKey(Category)
35 title = models.CharField(max_length=128) 32 title = models.CharField(max_length=128)
36 url = models.URLField(verify_exists=False, db_index=True) 33 url = models.URLField(verify_exists=False, db_index=True)
37 description = models.TextField(blank=True) 34 description = models.TextField(blank=True)
38 user = models.ForeignKey(auth.models.User) 35 user = models.ForeignKey(auth.models.User)
61 flagged_link = FlaggedLink(link = link, user = user, approved = False) 58 flagged_link = FlaggedLink(link = link, user = user, approved = False)
62 flagged_link.save() 59 flagged_link.save()
63 60
64 61
65 class FlaggedLink(models.Model): 62 class FlaggedLink(models.Model):
66 '''Model to represent links that have been flagged as broken by users''' 63 """Model to represent links that have been flagged as broken by users"""
67 link = models.ForeignKey(Link) 64 link = models.ForeignKey(Link)
68 user = models.ForeignKey(auth.models.User) 65 user = models.ForeignKey(auth.models.User)
69 date_flagged = models.DateField(auto_now_add = True) 66 date_flagged = models.DateField(auto_now_add = True)
70 approved = models.BooleanField(default = False, 67 approved = models.BooleanField(default = False,
71 help_text = 'Check this and save to remove the referenced link from the database') 68 help_text = 'Check this and save to remove the referenced link from the database')