django-sphinx 构建摘要
我正在尝试在 Django 的 Sphinx 中使用 BuildExcerpts。我的视图大概是这样的:
q = request.GET.get('q', '')
my_model_list = MyModel.search.query(q).set_options(passages=True, passages_opts={
'before_match':"<font color='red'>",
'after_match':'</font>',
'chunk_separator':' ... ',
'around':6,
})
当我运行这个的时候,出现了一个 AssertionError
错误。
这是错误的详细信息:
Traceback:
File "C:\Python25\lib\site-packages\django\core\handlers\base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "C:\django\myproject\myapp\views.py" in home_page
81. my_model_list = remove_duplicates(list(my_model_list))
File "c:\python25\lib\site-packages\django_sphinx-2.2.3-py2.5.egg\djangosphinx\models.py" in __iter__
243. return iter(self._get_data())
File "c:\python25\lib\site-packages\django_sphinx-2.2.3-py2.5.egg\djangosphinx\models.py" in _get_data
422. self._result_cache = list(self._get_results())
File "c:\python25\lib\site-packages\django_sphinx-2.2.3-py2.5.egg\djangosphinx\models.py" in _get_results
603. r['passages'] = self._get_passages(queryset[r['id']], results['fields'], words)
File "c:\python25\lib\site-packages\django_sphinx-2.2.3-py2.5.egg\djangosphinx\models.py" in _get_passages
657. passages_list = client.BuildExcerpts(docs, self._index, words, opts)
File "C:\Python25\lib\site-packages\django_sphinx-2.2.3-py2.5.egg\djangosphinx\apis\api278\__init__.py" in BuildExcerpts
791. assert(isinstance(doc, str))
Exception Type: AssertionError at /
Exception Value:
我不太明白发生了什么。有没有人遇到过类似的情况?
我使用的是 Django 1.2.3,Sphinx 0.9.9 和 django-sphinx 2.2.3。
1 个回答
1
对于遇到类似问题的其他人,这里是我解决问题的方法。
首先,找到你安装django-sphinx的文件夹,然后打开里面的models.py文件。在第650行,你需要把这两行代码替换成:
docs = [getattr(instance, f) for f in fields]
if isinstance(self._passages_opts, dict):
替换成
docs = [getattr(instance, f) for f in fields]
for index, doc in enumerate(docs):
if (not (isinstance(doc, str)) and (not isinstance(doc, unicode))):
docs[index] = repr(doc)
if isinstance(self._passages_opts, dict):
这样一来,你就可以在视图中像这样访问你的摘录:
for r in results_set:
print r.sphinx.get('passages')
或者在模板中这样访问:
{{record.sphinx.passages.content|safe}}