我希望做一个页面,使用django-haystack显示一组固定的搜索结果。我已经有了一个页面,可以使用haystack的SearchView进行搜索,并希望创建第二个页面,默认情况下显示特定搜索的结果--在本例中,搜索包含Fruit的所有文档。
这是我的urls.py:
from haystack.views import SearchView
from django.conf.urls import url
import haystack
from .search_Queries import custom_query
from .forms import CustomSearchForm
urlpatterns = [
url(r'^fruit/', SearchView(form_class=CustomSearchForm,
searchqueryset=custom_query)),
url(r'^$', SearchView(form_class=haystack.forms.SearchForm,
searchqueryset=custom_query),
]我试图通过覆盖表单中的get_queryset方法来使视图显示初始的Fruit搜索词--但是,这似乎不是正确的方法
forms.py
class CustomSearchForm(SearchForm):
def get_queryset(self):
queryset = super(CustomSearchForm, self).get_queryset()
queryset = queryset.auto_query('Fruit')
return queryset如何使用django-haystack在视图中显示一组针对特定术语的默认搜索结果?我想创建一个单独的视图,以便我的^fruit/ URL直接显示Fruit搜索结果,而不是重定向到/q=Fruit。
发布于 2017-12-14 05:43:55
一个带有SearchQuerySet功能的普通Django视图怎么样:
url(r'^myquery/$', views.custom_query),在views.py内部
from haystack.query import SearchQuerySet
from django.http JsonResponse
def custom_query(request):
sqs = SearchQuerySet().auto_query('fruit').load_all()
# and do whatever you want with the sqs
return JsonResponse({'result': sqs[0].object.pk})https://stackoverflow.com/questions/47473356
复制相似问题