Django 中的两个与搜索相关的错误:当没有搜索项时:“reduce() of empty sequence with no initial value”。在任何搜索词上:没有结果
Posted
技术标签:
【中文标题】Django 中的两个与搜索相关的错误:当没有搜索项时:“reduce() of empty sequence with no initial value”。在任何搜索词上:没有结果【英文标题】:Two search-related errors in Django: When no search-term: "reduce() of empty sequence with no initial value". On any search-term: No results 【发布时间】:2014-09-27 04:17:38 【问题描述】:我在使用 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]
【问题讨论】:
您有没有发现问题所在?我也有同样的问题 【参考方案1】:这个答案供那些使用 django-1.9 和 haystack-2.4.1 遇到类似错误的人参考。 我有同样的错误。 在此 *** 链接上找到了适用的答案 - “Django 1.9/Haystack 2.4.1 "Model could not be found for SearchResult"”
基本上,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
【讨论】:
以上是关于Django 中的两个与搜索相关的错误:当没有搜索项时:“reduce() of empty sequence with no initial value”。在任何搜索词上:没有结果的主要内容,如果未能解决你的问题,请参考以下文章
如何提高 Django 管理员搜索相关字段(MySQL)中的查询性能