带有 xapian 引擎的 Django-haystack : can't execute update_index if model has ManyToManyField

标签 django django-haystack xapian

将 django 升级到 1.7 后,我无法执行管理命令 update_index。

Traceback (most recent call last):
  File "/opt/pycharm-3.4.1/helpers/pydev/pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "/opt/pycharm-3.4.1/helpers/pydev/pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/tochium/projects/povary/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/tochium/projects/povary/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/tochium/projects/povary/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/tochium/projects/povary/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/tochium/projects/povary/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/tochium/projects/povary/src/django-haystack/haystack/management/commands/update_index.py", line 184, in handle
    return super(Command, self).handle(*items, **options)
  File "/home/tochium/projects/povary/local/lib/python2.7/site-packages/django/core/management/base.py", line 503, in handle
    label_output = self.handle_label(label, **options)
  File "/home/tochium/projects/povary/src/django-haystack/haystack/management/commands/update_index.py", line 189, in handle_label
    self.update_backend(label, using)
  File "/home/tochium/projects/povary/src/django-haystack/haystack/management/commands/update_index.py", line 234, in update_backend
    do_update(backend, index, qs, start, end, total, self.verbosity)
  File "/home/tochium/projects/povary/src/django-haystack/haystack/management/commands/update_index.py", line 89, in do_update
    backend.update(index, current_qs)
  File "/home/tochium/projects/povary/src/xapian-haystack/xapian_backend.py", line 355, in update
    data = index.full_prepare(obj)
  File "/home/tochium/projects/povary/src/django-haystack/haystack/indexes.py", line 207, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "/home/tochium/projects/povary/src/django-haystack/haystack/indexes.py", line 198, in prepare
    self.prepared_data[field.index_fieldname] = field.prepare(obj)
  File "/home/tochium/projects/povary/src/django-haystack/haystack/fields.py", line 159, in prepare
    return self.convert(super(CharField, self).prepare(obj))
  File "/home/tochium/projects/povary/src/django-haystack/haystack/fields.py", line 106, in prepare
    return current_object()
  File "/home/tochium/projects/povary/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 839, in __call__
    manager = getattr(self.model, kwargs.pop('manager'))
KeyError: u'manager'

我用的是python2.7.6,django 1.7,django-haystack 2.3.1,搜索引擎:xapian

我认为问题的出现是因为 1.7 中的 django 添加方法 调用 () 到 ManyRelatedManager ,现在在文件/haystack/fields.py 中,此条件会触发。
if callable(current_object):
    return current_object()

但在 调用 manager_name 应该被定义,我们如何在适当的方法中看到
    def __call__(self, **kwargs):
        # We use **kwargs rather than a kwarg argument to enforce the
        # `manager='manager_name'` syntax.
        manager = getattr(self.model, kwargs.pop('manager'))

谢谢。

最佳答案

使用自定义方法返回字符串或整数数组(如果您想在一个多值索引字段中存储更复杂的信息,请使用字符串模式)。

class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
    my_m2m_field = indexes.CharField()

    def prepare_my_m2m_field(self, my_model):
        return [m2m.prop for m2m in my_model.m2m_field.all()]

SOLR 的字段声明,包括用于分面和自动完成的字段:
<field name="my_m2m_field" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="my_m2m_field_exact" type="string" indexed="true" stored="false" multiValued="true"/>
<field name="my_m2m_field_ac" type="ngram" indexed="true" stored="true" multiValued="true"
       termVectors="true" termPositions="true" termOffsets="true"/>
<copyField source="my_m2m_field" dest="emotion_exact" />
<copyField source="my_m2m_field" dest="emotion_ac" />

关于带有 xapian 引擎的 Django-haystack : can't execute update_index if model has ManyToManyField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827744/

相关文章:

Django-Haystack + Whoosh - 重建索引后的空索引

python - 使用 Django-Haystack 添加到 solr 索引时出现问题

django - 如何获取haystack搜索中搜索结果的数量?

lucene - 部分词的文档搜索

python - Django,将 HTML 代码存储在数据库中

python - 在数据库插入/更新之前处理字段

javascript - 重用javascript代码的Django最佳实践

javascript - 为什么我不能提醒这个从 django render_to_response 返回的字符串