【问题标题】:django-haystack - Updating index after adding new field to index causing errordjango-haystack - 将新字段添加到索引后更新索引导致错误
【发布时间】:2012-01-19 04:26:09
【问题描述】:

我有一个 django 站点,它使用 Haystack 和 Xapian 后端进行搜索索引。我已向其中一个被索引的模型添加了一个新字段,然后将该字段添加到该模型的 SearchIndex 中。我跑了:

python manage.py update_index

要更新索引,但出现以下错误:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/management/commands/update_index.py", line 51, in handle
    self.handle_app(None, **options)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/management/commands/update_index.py", line 107, in handle_app
    index.backend.update(index, small_cache_qs[start:end])
  File "/usr/local/lib/python2.6/dist-packages/xapian_haystack-1.1.3beta-py2.6.egg/xapian_backend.py", line 204, in update
    data = index.prepare(obj)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/indexes.py", line 102, in prepare
    self.prepared_data[field_name] = field.prepare(obj)
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/fields.py", line 119, in prepare
    return self.convert(super(CharField, self).prepare(obj))
  File "/usr/local/lib/python2.6/dist-packages/django_haystack-1.0.1_final-py2.6.egg/haystack/fields.py", line 75, in prepare
    raise SearchFieldError("The model '%s' has an empty model_attr '%s' and doesn't allow a default or null value." % (repr(current_object), attr))
haystack.exceptions.SearchFieldError: The model 'None' has an empty model_attr 'address_county' and doesn't allow a default or null value.

我使用的版本是 django 1.2 和 django-haystack 1.0.1。目前,将这些升级到最新版本不是我的选择。

【问题讨论】:

  • 我解决了这个问题,在 search_indexes 中为 null 字段添加了null=True
  • @Guandalino:我认为我的问题与您当时的问题有些不同,正如您从下面的答案中看到的那样,解决方案正好相反

标签: django django-haystack


【解决方案1】:

我找到了答案。线索在错误消息中(众所周知,这并不总是发生!):

The model 'None' has an empty model_attr 'address_county' and doesn't allow a default or null value.

我的模型字段是用blank=True, null=True 创建的。这导致了错误,所以我删除了这些并添加了default='',这使我能够在没有错误的情况下更新索引。希望这对某人有所帮助!

【讨论】:

  • 你的意思是你改变了你的django模型?用“address_country”字段中的“default=''”替换“blank=True, null=True”?执行此操作后是否再次运行 ./manage.py build_solr_schema?
  • @harisibrahimkv:是的,我改变了我的 django 模型。但是,我没有运行该管理命令,因为我使用 Haystack 而不是 SOLR 作为搜索后端。但是,我确实为 Haystack 运行了等效的命令。我只是不记得那个命令是什么了。
  • 嗯。我尝试更改我的模型并再次运行 build_solr_schema。它没有帮助。我也在使用 Haystack,但没有偶然发现要运行任何特定命令以反映此更改。
  • @harisibrahimkv:抱歉,我有点搞混了。虽然我使用的是 Haystack,但我使用的搜索后端是 Xapian。你见过django-haystack.readthedocs.org/en/latest/tutorial.html#reindex 并尝试过重新索引。如果你有,或者如果它不起作用,我建议你问你自己的问题,因为我已经有几年没有使用这些(除了 Django)了。
  • 首选的方法应该是在索引中将相应字段标记为空,如stackoverflow.com/a/22695639/147021 中提到的记住,空白('')和空是不同的东西!
【解决方案2】:

在索引我的模态的电话字段时,我遇到了同样的问题。我只是将 null=True 添加到 search_index.py 中的 solr 字段

phone = CharField(model_attr="phone", null=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 2018-07-06
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 2017-07-02
    相关资源
    最近更新 更多