Вопрос

I was following the haystack tutorial line by line but when I tried to run the command rebuild_index I got an error AttributeError: 'CustomerIndex' object has no attribute 'fields'

I have double checked my settings file, haystack is there in my installed apps and my engine settings are also there.

I am using whoosh as my search engine. Version of haystack is 2.1.0 and of whoosh is 2.5.6

Please help me get rid of this error.

This is my search_indexes.py file:

import datetime
from haystack import indexes
from customers.models import Customer

class CustomerIndex(indexes.SearchField, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    first_name = indexes.CharField(model_attr='first_name')
    create_date = indexes.DateTimeField(model_attr='create_date')

    def get_model(self):
        return Customer

    def index_queryset(self, using=None):
        """
        used when the entire index model is updated
        """
        return self.get_model()._default_manager.\
              filter(create_date__lte=datetime.datetime.now())
Это было полезно?

Решение

The error is in the class attribute. Just replace SearchField by SearchIndex in the class attributes, change:

class CustomerIndex(indexes.SearchField, indexes.Indexable):

to:

class CustomerIndex(indexes.SearchIndex, indexes.Indexable):

Другие советы

Another reason that could cause that is using normal drf's default ModelViewSet instead of HaystackViewSet

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top