Reputation: 2736
Good afternoon,
Say I have a models.py like so:
class my_stackoverflow_question(models.Model):
feedback_choices = (
(GREAT, "Was a great question"),
(MEH, "Could've figured it out"),
(TERRIBLE, "I pity the foo"),
)
feedback = models.IntegerField(default=GREAT, choices=feedback_choices)
My search_indexes.py is such:
class question_index(indexes.SearchIndex, indexes.Indexable):
text = ...stuff...
feedback = indexes.IntegerField(model_attr='feedback', faceted=True)
When I display the above facet, the integer value will show through the template
{% for feed in facets.fields.feedback %}
{{feed.0}} - {{feed.1}}
{% endfor %}
# Shows: 0-999
# 1-1
# 2-0 ;)
I'd like feed.0 to display the actual choice value, like obj.get_feedback_display() would. So I thought I'd try to prepare the data before indexing:
def prepare_feedback(self, obj):
return obj.feedback.get_feedback_display() #'AttributeError: 'long' object has no attribute 'get_feedback_display''
or return "%s" % (obj.feedback.get_feedback_display()) #Same error as above
I'd even be fine if the data was indexed as an integer and then the feed.0 facet displayed the name -- but I believe the facet fields come straight from the index and not the model (is this correct?)
How can I display the facet's display_name as opposed to it's raw value?
Thank you!
Upvotes: 1
Views: 1488
Reputation: 2377
I think you're on the right track with indexing the display text instead of the integer choice value. You need to tell haystack that it should expect a string instead of a long for feedback, and then call get_feedback_display() on the my_stackoverflow_question
object itself, not the feedback field.
So:
class question_index(indexes.SearchIndex, indexes.Indexable):
text = ...stuff...
feedback = indexes.CharField(model_attr='feedback', faceted=True)
def prepare_feedback(self, obj):
return obj.get_feedback_display()
Upvotes: 3