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

Django-Haystack: 'NoneType' object has no attribute '_default_manager'

我正在尝试向我的 base.html 添加大海捞针搜索,以便将其全局包含在我的网站上。虽然当我提交搜索时出错

我得到:

Django-Haystack: 'NoneType' object has no attribute '_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。有用。更新版本的 django 有问题吗?

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

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

在 Haystack 中添加了对 Django 1.9 的支持 2.5 and support for Django 1.10 was added in Haystack 2.5.1

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

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

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

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