【问题标题】:django haystack error: AttributeError: object has no attribute 'fields'django haystack 错误:AttributeError:对象没有属性“字段”
【发布时间】:2014-01-22 04:04:52
【问题描述】:

我正在逐行遵循 haystack 教程,但是当我尝试运行命令 rebuild_index 时出现错误 AttributeError: 'CustomerIndex' object has no attribute 'fields'

我已经仔细检查了我的设置文件,我安装的应用程序中有 haystack,我的引擎设置也在那里。

我使用 whoosh 作为我的搜索引擎。 haystack 的版本是 2.1.0,whoosh 的版本是 2.5.6

请帮我摆脱这个错误。

这是我的 search_indexes.py 文件:

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())

【问题讨论】:

    标签: django django-haystack


    【解决方案1】:

    错误在类属性中。只需在类属性中将SearchField 替换为SearchIndex,更改:

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

    到:

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

    【讨论】:

    • 非常感谢!就是这样,我现在感觉好傻。我多次浏览 haystack 网站,每次都错过 SearchIndex。我需要更平静地思考错误。谢谢!
    【解决方案2】:

    另一个可能导致这种情况的原因是使用普通 drf 的默认 ModelViewSet 而不是 HaystackViewSet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2013-06-29
      • 1970-01-01
      • 2016-04-23
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多