django-haystack:object没有属性'Indexable'

时间:2012-02-02 14:05:06

标签: django django-haystack

我是django-haystack的新手,我正在尝试按照入门指南进行操作。但是,我遇到了一个AttributeError:对象没有属性'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。

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

1 个答案:

答案 0 :(得分:4)

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

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

应该是(正确导入SearchIndex):

class BayModelIndex(SearchIndex):

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

相关问题