From 7740834baeccd835a1b17a0fe3b3d6a3a5a7b007 Mon Sep 17 00:00:00 2001 From: Charles Holbrow Date: Wed, 27 Feb 2013 18:58:38 -0500 Subject: [PATCH] Adding redundant school_name field to courses to enable faster server side filtering. requires: for c in Course.objects.all(): c.save() --- karmaworld/apps/courses/models.py | 6 +++++- karmaworld/apps/courses/views.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index 40deedc..882db84 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -75,8 +75,10 @@ class Course(models.Model): instructor_name = models.CharField(max_length=255, blank=True, null=True) instructor_email = models.EmailField(blank=True, null=True) - updated_at = models.DateTimeField(default=datetime.datetime.utcnow) + # Save school name redundantly to speed filtering + school_name = models.CharField(max_length=255, blank=True, null=True) + updated_at = models.DateTimeField(default=datetime.datetime.utcnow) created_at = models.DateTimeField(auto_now_add=True) @@ -92,6 +94,8 @@ class Course(models.Model): def save(self, *args, **kwargs): """ Save school and generate a slug if one doesn't exist """ + if self.school_name != self.school.name: + self.school_name = self.school.name super(Course, self).save(*args, **kwargs) # generate a self.id if not self.slug: self.slug = defaultfilters.slugify("%s %s" % (self.name, self.id)) diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index c704bfb..08159d8 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -78,7 +78,8 @@ class CourseAjaxList(BaseListView): queries = [ ( Q(name__icontains=s) | - Q(instructor_name__icontains=s) + Q(instructor_name__icontains=s) | + Q(school_name__icontains=s) ) for s in search_strings ] -- 2.25.1