【问题标题】:Django-Haystack: Preparing index data that has choicesDjango-Haystack:准备有选择的索引数据
【发布时间】:2014-03-27 16:17:43
【问题描述】:

下午好,

假设我有一个这样的models.py:

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)

我的 search_indexes.py 是这样的:

class question_index(indexes.SearchIndex, indexes.Indexable):
    text = ...stuff...
    feedback = indexes.IntegerField(model_attr='feedback', faceted=True)

当我显示上面的分面时,整数值将通过模板显示

{% for feed in facets.fields.feedback %}
    {{feed.0}} - {{feed.1}}
{% endfor %}

# Shows: 0-999
#        1-1
#        2-0 ;)

我希望 feed.0 显示实际的选择值,就像 obj.get_feedback_display() 那样。所以我想我会尝试在索引之前准备数据:

def prepare_feedback(self, obj):
    return obj.feedback.get_feedback_display()  #'AttributeError: 'long' object has no attribute 'get_feedback_display''

或 return "%s" % (obj.feedback.get_feedback_display()) #同上报错

如果数据被索引为整数,然后 feed.0 分面显示名称,我什至会很好——但我相信分面字段直接来自索引而不是模型(这是正确的吗?)

如何显示构面的 display_name 而不是它的原始值?

谢谢!

【问题讨论】:

    标签: django django-haystack faceted-search facets


    【解决方案1】:

    我认为您在索引显示文本而不是整数选择值方面走在正确的轨道上。您需要告诉 haystack 它应该期待一个字符串而不是 long 用于反馈,然后在 my_stackoverflow_question 对象本身而不是反馈字段上调用 ​​get_feedback_display()。

    所以:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多