python
  • django
  • django-sphinx
  • 2010-12-01 28 views 0 likes 
    0

    我想在django sphinx中使用BuildExcerpts。我的觀點看起來是這樣的: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

    對於其他有類似問題的人來說,這是我必須要解決的問題。

    轉到您的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}} 
    
    +0

    看起來像來源對於此修復程序,請訪問https://groups.google.com/forum/#!msg/django-sphinx/WNL3a5h3MU8/nlGpujbfMEYJ – mlissner 2011-03-13 08:30:56

    相關問題