现在我有了问题的答案。haystack 附带的标准搜索不支持按子字符串搜索。
我创建了自己的应用程序(mysearch)用于搜索,它将使用 haystacksearch
您要做的就是创建自己的 SearchForm (mysearch/forms.py)
from django import forms
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet
class AutocompleteSearchForm(ModelSearchForm):
def search(self):
if not self.is_valid():
return self.no_query_found()
if not self.cleaned_data.get('q'):
return self.no_query_found()
sqs = self.searchqueryset.filter(autocompletetext=self.cleaned_data.get('q'))
sqs = sqs.models(*self.get_models())
if self.load_all:
sqs = sqs.load_all()
return sqs
然后你必须使用你自己的 SearchForm (mysearch/urls.py) 创建你自己的 url-route
from django.conf.urls.defaults import *
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView
from forms import AutocompleteSearchForm
sqs = SearchQuerySet()
# Without threading...
urlpatterns = patterns('haystack.views',
url(r'^$', SearchView(searchqueryset=sqs, form_class=AutocompleteSearchForm), name='haystack_search'),
)
在您的项目中使用 urls.py
url(r"^search/", include("mysearch.urls", namespace="Search")),
代替
url(r'^search/', include('haystack.urls', namespace="Search")),