【问题标题】:Django - haystack - jinja2, multivaluefield tagsDjango - haystack - jinja2,多值字段标签
【发布时间】:2016-06-09 15:10:48
【问题描述】:

我正在使用带有 whoosh 后端的 haystack。我的模型中有用于标签的 django-taggit。

search_indexes.py:

class WorkIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    tags = indexes.MultiValueField()

    def prepare_tags(self, obj):
     return [tag.name for tag in obj.tags.all()] 

    def get_model(self):
        return Work

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.all()

work_text.txt:

{{ object.title }}
{{ object.description }}
{% for tag in tags %} {{ tag }} {% endfor %}

但是当我通过标签搜索作品时,我看不到结果?我必须注意我正在使用 jinja2。

谢谢

【问题讨论】:

    标签: django django-haystack


    【解决方案1】:

    正确的模板是:

    work_text.txt

    {{ object.title }}
    {{ object.description }}
    {% for tag in object.tags.all() %}{{ tag.name }} {% endfor %}
    

    【讨论】:

    • 这是object.tags.all 而不是object.tags.all()
    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多