Haystack: Fix error building index to a null fields

I’m using haystack on my django project using Whoosh, and got some trouble once building index for the haystack and below is my haystack index definition.

class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    description = indexes.CharField(model_attr='description')

    def get_model(self):
        return Item

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(created__lte=timezone.now())

And I got an error once building index using “./manage.py rebuld_index”

raise SearchFieldError("The model '%s' combined with model_attr '%s' returned None, but doesn't allow a default or null value." % (repr(obj), self.model_attr))
haystack.exceptions.SearchFieldError: The model '<Item: MyData>' combined with model_attr 'description' returned None, but doesn't allow a default or null value

Haystack has pretty good error information that “description” field not allowed to be null to be indexed on haystack. Then I fix this just add “null=True” argument following.

description = indexes.CharField(model_attr='description', null=True)

Reference: http://stackoverflow.com/a/22695639/1936697

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s