# HG changeset patch # User Brian Neal # Date 1270260904 0 # Node ID fa7d82bfb100f162efc6635d198354b1e85cbd82 # Parent 341759e1cda1038d6fd5ac1d9e07a3a75db7b3ed Implement #68: add a denormalized count field to weblinks Category model to reduce database queries. diff -r 341759e1cda1 -r fa7d82bfb100 gpp/templates/weblinks/index.html --- a/gpp/templates/weblinks/index.html Sat Apr 03 01:10:00 2010 +0000 +++ b/gpp/templates/weblinks/index.html Sat Apr 03 02:15:04 2010 +0000 @@ -7,7 +7,7 @@
{% for category in categories %}
{{ category.title }} - ({{ category.num_links}})
+ ({{ category.count }})

{{ category.description }}

{% endfor %}
diff -r 341759e1cda1 -r fa7d82bfb100 gpp/weblinks/__init__.py --- a/gpp/weblinks/__init__.py Sat Apr 03 01:10:00 2010 +0000 +++ b/gpp/weblinks/__init__.py Sat Apr 03 02:15:04 2010 +0000 @@ -0,0 +1,1 @@ +import signals diff -r 341759e1cda1 -r fa7d82bfb100 gpp/weblinks/admin.py --- a/gpp/weblinks/admin.py Sat Apr 03 01:10:00 2010 +0000 +++ b/gpp/weblinks/admin.py Sat Apr 03 02:15:04 2010 +0000 @@ -5,6 +5,12 @@ from weblinks.models import Link from weblinks.models import FlaggedLink + +class CategoryAdmin(admin.ModelAdmin): + list_display = ('title', 'description', 'count') + readonly_fields = ('count', ) + + class LinkAdmin(admin.ModelAdmin): list_display = ('title', 'url', 'category', 'date_added', 'hits', 'is_public') list_filter = ('date_added', 'is_public', 'category') @@ -14,11 +20,12 @@ raw_id_fields = ('user', ) save_on_top = True + class FlaggedLinkAdmin(admin.ModelAdmin): list_display = ('__unicode__', 'url', 'get_link_url', 'user', 'date_flagged') date_hierarchy = 'date_flagged' raw_id_fields = ('user', ) -admin.site.register(Category) +admin.site.register(Category, CategoryAdmin) admin.site.register(Link, LinkAdmin) admin.site.register(FlaggedLink, FlaggedLinkAdmin) diff -r 341759e1cda1 -r fa7d82bfb100 gpp/weblinks/models.py --- 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)