Mercurial > public > sg101
changeset 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 | 341759e1cda1 |
children | 6a5549c2efb5 |
files | gpp/templates/weblinks/index.html gpp/weblinks/__init__.py gpp/weblinks/admin.py gpp/weblinks/models.py |
diffstat | 4 files changed, 18 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ <dl> {% for category in categories %} <dt><a href="{% url weblinks-view_links category=category.id,sort="title",page=1 %}">{{ category.title }}</a> - ({{ category.num_links}})</dt> + ({{ category.count }})</dt> <dd><p>{{ category.description }}</p></dd> {% endfor %} </dl>
--- 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
--- 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)
--- 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)