2011-12-13 110 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