diff 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
line wrap: on
line diff
--- a/gpp/weblinks/models.py	Sat Apr 03 01:10:00 2010 +0000
+++ b/gpp/weblinks/models.py	Sat Apr 03 02:15:04 2010 +0000
@@ -1,22 +1,19 @@
-'''
+"""
 This module contains the models for the weblinks application.
-'''
-
+"""
 from django.db import models
 from django.contrib import auth
 
 
 class Category(models.Model):
-    '''Links belong to categories'''
-    title = models.CharField(max_length = 64)
-    description = models.TextField(blank = True)
+    """Links belong to categories"""
+    title = models.CharField(max_length=64)
+    description = models.TextField(blank=True)
+    count = models.IntegerField(default=0)
 
     def __unicode__(self):
         return self.title
 
-    def num_links(self):
-        return Link.public_objects.filter(category = self.pk).count()
-
     class Meta:
         verbose_name_plural = 'Categories'
         ordering = ('title', )
@@ -30,7 +27,7 @@
 
 
 class Link(models.Model):
-    '''Model to represent a web link'''
+    """Model to represent a web link"""
     category = models.ForeignKey(Category)
     title = models.CharField(max_length=128)
     url = models.URLField(verify_exists=False, db_index=True)
@@ -63,7 +60,7 @@
 
 
 class FlaggedLink(models.Model):
-    '''Model to represent links that have been flagged as broken by users'''
+    """Model to represent links that have been flagged as broken by users"""
     link = models.ForeignKey(Link)
     user = models.ForeignKey(auth.models.User)
     date_flagged = models.DateField(auto_now_add = True)