我使用AJAX和haystack.query.SearchQuerySet
在Django(版本1.7,Windows 7,32位)中收到两个与搜索相关的错误。
首先,当搜索框中没有任何内容时(或者您只键入空格):
Exception Value: reduce() of empty sequence with no initial value
问题行是此视图中的SearchQuerySet().autocomplete(...)
行(views.py
中的函数):
def search_titles(request):
articles = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
return render_to_response('ajax_search.html', {'articles' : articles})
(SearchQuerySet
是haystack.query
的一部分。)
回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/articles/search/
Django Version: 1.7c2
Python Version: 3.4.1
Installed Applications:
('article',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.formtools',
'userprofile',
'whoosh',
'haystack')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "c:\applications\programming\python_341\Lib\site-packages\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test\article\views.py" in search_titles
111. articles = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
File "c:\applications\programming\python_341\Lib\site-packages\haystack\query.py" in autocomplete
463. return clone.filter(six.moves.reduce(operator.__and__, query_bits))
Exception Type: TypeError at /articles/search/
Exception Value: reduce() of empty sequence with no initial value
另一个问题是,当搜索框中有任何文本时,它不会崩溃,但它永远不会找到任何内容。
SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
始终返回空文章对象(print(str(articles))
为[]
)。
settings.py
中的相关设置:
WHOOSH_INDEX = os.path.join(BASE_DIR, "whoosh/")
HAYSTACK_CONNECTIONS = {
"default": {
"ENGINE" : "haystack.backends.whoosh_backend.WhooshEngine",
"PATH" : WHOOSH_INDEX,
},
}
在应用的urls.py
中:
url(r'^search/$', 'article.views.search_titles'),
另外,这里是search_indexes.py
:
from haystack import indexes
from article.models import Article
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
pub_date = indexes.DateTimeField(model_attr='pub_date')
content_auto = indexes.EdgeNgramField(model_attr='title')
def get_model(self):
return Article
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
print("ai.6 self.get_model().objects.all()=" + str(self.get_model().objects.all()) + "")
return self.get_model().objects.all()
python manage.py rebuild_index
的输出:
[R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test]python manage.py
rebuild_index
WARNING: This will irreparably remove EVERYTHING from your search index in connection 'd
efault'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` c
ommand.
Are you sure you wish to continue? [y/N] y
Removing all documents from your index because you said so.
All documents removed.
ai.6 self.get_model().objects.all()=[<Article: Test1 Titlee>, <Article: Test2 Title>, <
Article: Test3 Title>, <Article: Article 4 Title>, <Article: 5>, <Article: 1348 article
title>, <Article: bla>]
Indexing 7 articles
[R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test]
答案 0 :(得分:0)
这个答案是为了参考那些在django-1.9和haystack-2.4.1中遇到类似错误的人。 我有同样的错误。 找到适用于此stackoverflow链接的答案 - &#34; Django 1.9/Haystack 2.4.1 "Model could not be found for SearchResult"&#34;
基本上,haystack库2.4.1有一些问题,现在已经在github存储库源代码中修复了。 pip install
可以从github完成:
pip install -e git+git://github.com/django-haystack/django-haystack.git#egg=django-haystack
pip freeze --local git+git://github.com/django-haystack/django-haystack.git#egg=django-haystack > requirements.txt