Django-Haystack:' NoneType'对象没有属性' _default_manager'

时间:2016-01-18 10:27:04

标签: django django-haystack django-1.9

我正在尝试将大海捞针搜索添加到我的base.html,以便在我的网站上全局包含它。但是当我提交搜索时它会出错

我得到了:

  

Django-Haystack:' NoneType'对象没有属性' _default_manager'

我已将其添加到INSTALLED_APPS和我的urls.py. ./manage.py rebuild_index运行正常。

models.py:

class Site(models.Model):
    site_name = models.CharField(max_length=100, blank=False, null=False)
    site_manager = models.CharField(max_length=100, blank=False, null=False)
    address_1 = models.CharField(max_length=100, blank=False, null=False)
    address_2 = models.CharField(max_length=100, blank=True, null=True)
    address_3 = models.CharField(max_length=100, blank=True, null=True)
    town_city = models.CharField(max_length=100, verbose_name='Town/City')
    county = models.CharField(max_length=100, blank=False, null=False)
    postcode = models.CharField(max_length=100, blank=False, null=False)
    region = models.CharField(max_length=100, blank=True, null=True)
    tel = models.CharField(max_length=100, blank=False, null=False)
    email = models.EmailField(max_length=100, blank=False, null=False)

    creation = models.DateField(auto_now_add=True)
    last_modified = models.DateField(auto_now=True)

    def __unicode__(self):
        return self.site_name

SiteIndex.py:

class SiteIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    site_name = indexes.CharField(model_attr='site_name')
    site_manager = indexes.CharField(model_attr='site_manager')
    address_1 = indexes.CharField(model_attr='address_1')
    address_2 = indexes.CharField(model_attr='address_2')
    address_3 = indexes.CharField(model_attr='address_3')
    town_city = indexes.CharField(model_attr='town_city')
    county = indexes.CharField(model_attr='county')
    postcode = indexes.CharField(model_attr='postcode')
    region = indexes.CharField(model_attr='region')
    tel = indexes.CharField(model_attr='tel')
    email = indexes.CharField(model_attr='email')
    creation = indexes.DateField(model_attr='creation')
    last_modified = indexes.DateField(model_attr='last_modified')

    def get_model(self):
        return Site

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

site_text.txt:

{{ object.site_name }}
{{ object.site_manager }}
# ect

base.html文件

<form action="/search/" method="get">
     <input type="text" name="q">
     <input type="submit" value="Search">
</form>

更新:

我注意到,如果我降级到DJango 1.8。有用。有更多uptodate版本的django是否存在问题?

3 个答案:

答案 0 :(得分:8)

错误是因为您的Haystack版本不支持您的Django版本。如果你想使用Django 1.9 +

,你需要升级你的django-haystack版本

在Haystack 2.5中添加了对Django 1.9的支持,并在Haystack 2.5.1中添加了对Django 1.10的支持。

答案 1 :(得分:4)

您可以从github安装最新版本(如信号回答),但使用pip:

pip uninstall django-haystack
pip install -e git+https://github.com/django-haystack/django-haystack.git#egg=django-haystack

答案 2 :(得分:2)

我通过安装github的最新版本(目前为2.5.dev0)解决了这个问题:

pip uninstall django-haystack
git clone https://github.com/django-haystack/django-haystack
cd django-haystack
python setup.py install