Currently I have a database of Courses, and it has 6 columns:
class Course(models.Model):
title = models.CharField(max_length=50)
number = models.CharField(max_length=12)
Cat1 = models.BooleanField()
Cat2 = models.BooleanField()
Cat3 = models.BooleanField()
department = models.ForeignKey(Department)
The unicode method for this course is made to output something like:
def __unicode__(self):
return u'%s %s %s %s %s %s' % (self.department, self.number, self.title, 'CAT1' if self.Cat1 else '','Cat2' if self.Cat2 else '','Cat3' if self.Cat3 else '')
Which returns something like: ENG 1104 Academic Writing Cat1 Cat3
I’m trying to implement a search to find courses by text queries, so I tried Haystack (with Whoosh as the engine), but to make it easier to index I just added a new column to the model named text, in which I just added the Unicode text of each course. This method works pretty well, but obviously its not robust and because this is only a small project of me for learning purposes, I would like to know more efficient ways of accomplishing this search. Any Ideas?
Haystack doesn’t require any changes to your models. The recommended method is to use a template:
Then, you create a file named in the form of
templates/search/indexes/[app]/[model]_[indexfield].txt. So, in this case it would betemplates/search/indexes/yourapp/course_text.txt. Then, you use standard Django template tags and such to create a plain text representation of your model:Add as much info as you like. If you have any rich text fields that include HTML, remember to use
striptags, so you don’t end up with garbage in your index.