自定义视图不会在带有弹性搜索的Django Haystack中显示结果

时间:2017-08-07 21:54:08

标签: python django elasticsearch django-views django-haystack

我开始在Elasticsearch上使用Django Haystack。

直到我开始按照readthedocs中的简单示例制作自定义视图。

search_indexes.py:

class ExperimentIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    owner = indexes.CharField(model_attr='owner')

    def get_model(self):
        return Experiment

    def index_queryset(self, using=None):
        return self.get_model().lastversion_objects.all()

urls.py:

url(r'^search/?$', NepSearchView.as_view(), name='search_view')
之前

urls.py(没有自定义视图):

url(r'^search/', include('haystack.urls'))

views.py

class NepSearchView(SearchView):

    def get_queryset(self):
        queryset = super(NepSearchView, self).get_queryset()
        if not self.request.user.is_authenticated and \
                self.request.user.groups.filter(name='trustees').exists():
            return queryset  # (with some filter)
        else:
            return queryset

search.html:

{# ... #}
{% for result in page.object_list %}
{% if result.model_name == 'experiment' %}
{% include 'search/experiments.html' %}
{% endif %}
{% if result.model_name == 'study' %}
{% include 'search/studies.html' %}
{% endif %}
{% if result.model_name == 'group' %}
{% include 'search/groups.html' %}
{% endif %}
{% if result.model_name == 'experimentalprotocol' %}
{% include 'search/experimental_protocol.html' %}
{% endif %}
{# ... #}

嗯,事实是当使用默认的Haystack SearchView我得到了正确的匹配,而在介绍NepSearchView时,page.object_list是空的,我得到了{{1}在模板中。

我已经运行了No results found.,在网络上进行了大量搜索,但无法找到解释我失踪的内容。

0 个答案:

没有答案