【问题标题】:django-haystack: object has no attribute 'Indexable'django-haystack:对象没有“可索引”属性
【发布时间】:2012-02-03 00:42:55
【问题描述】:

我是 django-haystack 的新手,我正在尝试遵循入门指南。但是,我遇到了 AttributeError: object has no attribute 'Indexable'。

在我的 settins.py 中,我有:

HAYSTACK_SITECONF = 'mysite.search_sites'
HAYSTACK_SEARCH_ENGINE = 'solr'
HAYSTACK_SOLR_URL = 'http://127.0.0.1:8983/solr'

在我的 models.py(位于我的名为“searchapp”的应用中)中,我有:

from django.db import models
from django.contrib.auth.models import User


class baymodel(models.Model):
    id = models.IntegerField(primary_key=True)
    domain = models.CharField(max_length=765, db_column='Domain', blank=True)
    category = models.CharField(max_length=765, db_column='Category', blank=True) 
    link = models.CharField(max_length=765, db_column='Link') 
    name = models.CharField(max_length=765, db_column='Name', blank=True) 
    cur_timestamp = models.DateTimeField()

    def __unicode__(self):
      return self.name

    def index_queryset(self):
    """Used when the entire index for model is updated."""
       return self.objects.all()

在我的 search_indexes.py(位于我的 searchapp 目录中)中,我有:

import datetime
from haystack import indexes
from searchapp.models import baymodel


class baymodelIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    name = indexes.CharField(model_attr='user')
    link = indexes.CharField(model_attr='link')
    domain = indexes.CharField(model_attr='domain')
    pub_date = indexes.DateTimeField(model_attr='cur_timestamp')

def get_model(self):
    return baymodel

site.register(baymodel, baymodelIndex)

在 search_sites.py 中,我有:

import haystack
haystack.autodiscover()

我已经按照他们的说明安装了 solr,我可以看到漂亮的 solr 管理页面。

现在,当我这样做时:

sudo python manage.py build_solr_schema

我得到一个 AttributeError:

AttributeError: 'module' object has no attribute 'Indexable'

我已经尝试过:

python ./manage.py shell

我又得到了:

AttributeError: 'module' object has no attribute 'Indexable'

如果我只是进入 python 并尝试导入 haystack,我会得到:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import haystack
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/haystack/__init__.py", line 26, in <module>
raise ImproperlyConfigured("You must define the HAYSTACK_SITECONF setting before using the search framework.")
django.core.exceptions.ImproperlyConfigured: You must define the HAYSTACK_SITECONF  setting before using the search framework.

这很奇怪,因为我的 settings.py 确实指定了 HAYSTACK_CONF 并且 python ./manage.py shell 会抛出一个 AttributeError。

有没有人遇到过类似的错误?谢谢。

【问题讨论】:

  • 你安装了哪个版本?看起来您已经安装了最新版本,但正在使用开发文档 - 所以它应该是 class BayModelIndex(SearchIndex): intsead - readthedocs.org/docs/django-haystack/en/v1.2.4/…
  • 嗨詹姆斯:当我查看 .egg 文件时,我看到版本:1.2.6。当我使用 BayModelIndex(SearchIndex) 时,它会在 SearchIndex [NameError: name 'SearchIndex' is not defined] 上引发 NameError。
  • 您是否正确导入了 SearchIndex?从 haystack.indexes 导入 SearchIndex
  • 很高兴它有帮助。在 BayModelIndex 中,您似乎没有定义 index_queryset,它获取对象列表?
  • 你会想要 BayModel.objects 而不是 'baymodelIndex.objects。它只需要返回一个普通的 orm 查询。

标签: django django-haystack


【解决方案1】:

代码基于正在开发的 haystack 2,v2 改变了索引的定义方式。安装的版本是 1.2,所以正确的文档是available here,例如

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

应该是(正确导入 SearchIndex):

class BayModelIndex(SearchIndex):

索引类也需要定义index_queryset,而不是get_model,它应该返回一个QuerySet。

【讨论】:

  • 这很奇怪,当我点击最新时,我仍然得到旧文档。我已经明确点击了 1.2.7
猜你喜欢
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
相关资源
最近更新 更多