I’ve got a Ticket model that I’m trying to search by its IntegerField priority attribute as a string. I can get haystack+solr to search by the integer value, but not as a string.
I thought you could do this with a prepare_priority function in the search index class, but I’m not having any luck. Here’s my search_index.py:
from haystack import indexes
from helpdesk.models import Ticket
class TicketIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
priority = indexes.CharField(model_attr='priority')
def prepare_priority(self, obj):
priorities = {1:'critical', 2:'high', 3:'normal', 4:'low'}
return priorities[obj.priority]
def get_model(self):
return Ticket
def index_queryset(self):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
Here’s my ticket_text.txt template:
{{ object.title }}
{{ object.priority }}
{{ object.body }}
Am I misunderstanding something or doing something wrong?
Thanks.
Why
prepare_priority()“doesn’t work”A
SearchIndexis used to convert individual objects of a given model into entries in your Whoosh index. I’ll call these entriesSearchResults, since that’s how they show up in the shell. Now, the fields you defined inTicketIndexare (of course) present on theSearchResultsfor theTickets:That means that
'priority'shows up twice–once as independent field and once as part of the content of'text'. The independent field does go through the prepare_X procedure. The text field goes through a template (whereobjectrefers to the original model object, not a dictionary of prepared data).Fixing it
In the models:
Use the automatically provided
get_priority_displayas themodel_attr:Use
get_priority_displayin the template: