2011-12-13 58 views
4

我有一个django网站,它使用Haystack和Xapian后端进行搜索索引。我为其中一个正在索引的模型添加了一个新字段,然后将该字段添加到该模型的SearchIndex中。我已经运行:django-haystack - 将新字段添加到索引后更新索引导致错误

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. 

我使用的版本是1.2的Django Django的和,草垛1.0。 1。将这些升级到最新版本目前不适合我。

+3

我解决了这个问题,在search_indexes中为空字段添加了'null = True'。 – Paolo

+0

@Guandalino:我认为我的问题一定与你的问题有所不同,正如你从下面的答案中看到的解决方案是相反的 – hellsgate

回答

10

我找到了答案。线索是在错误信息(其中,因为我们都知道,并不总是发生!):

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

我的模型领域一直与blank=True, null=True创建。这导致了错误,所以我删除了这些并添加了default='',这使我能够更新索引而没有错误。希望这可以帮助别人!

+0

你的意思是你改变了你的django模型吗?在“address_country”字段中将“blank = True,null = True”替换为“default =''”?这样做后你又运行了./manage.py build_solr_schema吗? – harisibrahimkv

+0

@harisibrahimkv:是的,我改变了我的django模型。但是,我没有运行该管理命令,因为我在使用Haystack而不是我的搜索后端使用SOLR。不过,我的确在Haystack上运行了相应的命令。我不记得那个命令是什么了。 – hellsgate

+0

嗯。我试着改变我的模型并再次运行build_solr_schema。它没有帮助。我也在使用Haystack,但还没有偶然发现任何特定的命令来运行此更改来反映。 – harisibrahimkv