带有xapian引擎的Django干草堆:如果模型具有ManyToManyField,则无法执行update_index [英] Django-haystack with xapian engine: can't execute update_index if model has ManyToManyField

查看:121
本文介绍了带有xapian引擎的Django干草堆:如果模型具有ManyToManyField,则无法执行update_index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After upgrading django to 1.7 I can't execute management command 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

I use python2.7.6, django 1.7, django-haystack 2.3.1, search engine: xapian

我认为出现问题是因为Django从1.7开始将方法调用()添加到ManyRelatedManager,现在我

I think the problem appears because django from 1.7 add method call () to ManyRelatedManager and now in file /haystack/fields.py this condition fires.

if callable(current_object):
    return current_object()

但是在通话中应该定义manager_name,我们如何才能适当地看到方法

But in call manager_name should be defined, how we can see in appropriate method

    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'))

谢谢。

推荐答案

使用自定义方法返回字符串或整数数组(如果要存储更复杂的字符串,请使用字符串模式)

Use a custom method to return an array of strings or integers (use string patterns if you want to store more complex information in one multiValued index field).

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 declaration for SOLR including fields for faceting and autocomplete:

<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干草堆:如果模型具有ManyToManyField,则无法执行update_index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆