Django-Haystack给出属性错误?

时间:2016-01-03 12:32:56

标签: python django django-haystack whoosh

我正在尝试使用我的Django应用程序使用Haystack和Whoosh。我按照Haystack文档中的步骤进行操作,但是当我进行搜索时,我收到此错误

AttributeError at /search/
'module' object has no attribute 'get_model'

search_indexes.py -

import datetime
from haystack import indexes
from movies.models import Movie


class MovieIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')

    def get_model(self):
        return Movie

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

我无法在任何地方找到这个错误的帮助,我做错了什么?

Stacktrace -

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/search/?q=The+Revenant&models=movies.movie

Django Version: 1.9.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'whoosh',
 'haystack',
 'registration',
 'crispy_forms',
 'movies',
 'mptt')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')



Traceback:

File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/views.py" in __call__
  51.         self.results = self.get_results()

File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/views.py" in get_results
  91.         return self.form.search()

File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/forms.py" in search
  116.         return sqs.models(*self.get_models())

File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/forms.py" in get_models
  110.                 search_models.append(models.get_model(*model.split('.')))

Exception Type: AttributeError at /search/
Exception Value: 'module' object has no attribute 'get_model'

Python 2.7.6
Django 1.9.1
干草堆2.4.1
飞快移动2.7.0

3 个答案:

答案 0 :(得分:6)

在我看来,这就像是Haystack和Django版本之间的兼容性问题。 Django最近重新设计了get_model系统(我认为在1.9中),所以如果你升级了Django而不是Haystack - 或者反之亦然 - 这可能是问题所在。

我猜你的索引文件中的get_model()个引用是一个潜在的红色鲱鱼,而且问题出在Haystack内部,因为它无法找到它所期望的方法。

答案 1 :(得分:6)

我在haystack \ forms.py中替换了以下行:

if self.is_valid():
        for model in self.cleaned_data['models']:
            search_models.append(models.get_model(*model.split('.')))

由此:

if self.is_valid():
        for model in self.cleaned_data['models']:
            model_info = model.split('.')
            search_models.append(apps.get_model(app_label=model_info[0], model_name=model_info[1]))

使用导入

from django.apps import apps

我发现只需按照我收到的错误调用堆就可以更改该行。对你而言,似乎是在" /home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/forms.py"

编辑:获取最新版本的haystack解决了这个问题

pip uninstall django-haystack
pip install git+https://github.com/django-haystack/django-haystack.git

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

答案 2 :(得分:0)

只需在搜索索引类中定义一个新方法(search_indexes.py)

from .models import mymodel
from haystack import indexes
class MyIndex(indexes.SearchIndex,indexes.Indexable):

     def get_model(self):
        return mymodel