Django-haystack - Updating an index after adding a new field to the index causing the error

I have a django site that uses Haystack with a Xapian database to index the search. I added a new field for one of the indexed models, then added this field to SearchIndex for this model. I ran:

python manage.py update_index

To update the index, but I get the following error:

 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. 

I am using django 1.2 and django-haystack 1.0.1. Upgrading to the latest version is not an option for me now.

+5
source share
2 answers

I have found the answer. The hint was in the error message (which, as we all know, does not always happen!):

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

My model field was created using blank=True, null=True . This caused an error, so I deleted those and added default='' , and this allowed me to update the index without errors. Hope this ever helps someone!

+11
source

I encountered the same problem when indexing the phone field in my modal. I just add null = True to the solr field in search_index.py

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

0
source

Source: https://habr.com/ru/post/1386058/


All Articles