Django + Haystack:例外价值:'模块'对象没有属性' get_model'

时间:2016-04-14 06:29:31

标签: django-haystack whoosh

我设置了一个非常简单的项目来使用Whoosh引擎尝试Haystack,主要是遵循文档中的示例。我使用pip安装了所有内容,没有版本号,所以我应该有最新版本。

我收到了这个错误,我不知道我现在应该做什么,虽然我已经搜索过Google,但我找不到类似的东西。请帮忙!

项目文件夹结构非常简单,有一个名为cat的应用程序。

    project
    |-cat
    |  |-migrations
    |  |-admin.py, apps.py, models.py, search_indexes.py
    |-templates
    |  |-search
    |     |-indexes
    |     |  |-cat
    |     |     |-cat_text.txt
    |     |-search.html
    |-manage.py, settings.py, urls.py

我得到的错误是:

    Environment:


    Request Method: GET
    Request URL: http://localhost:8000/search/?q=felix&models=cat.cat

    Django Version: 1.9.4
    Python Version: 2.7.10
    Installed Applications:
    ['django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'haystack',
     'cat',]
    Installed Middleware:
    ['django.middleware.security.SecurityMiddleware',
     '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']



    Traceback:

    File "/Users/lebouuski/projects/django/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
      149.                     response = self.process_exception_by_middleware(e, request)

    File "/Users/lebouuski/projects/django/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
      147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "/Users/lebouuski/projects/django/lib/python2.7/site-packages/haystack/views.py" in __call__
      51.         self.results = self.get_results()

    File "/Users/lebouuski/projects/django/lib/python2.7/site-packages/haystack/views.py" in get_results
      91.         return self.form.search()

    File "/Users/lebouuski/projects/django/lib/python2.7/site-packages/haystack/forms.py" in search
      116.         return sqs.models(*self.get_models())

    File "/Users/lebouuski/projects/django/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'

models.py:

class Cat(models.Model):
  name = models.CharField(max_length=255)
  birth_date = models.DateField(default=datetime.date.today)
  bio = models.TextField(blank=True)
  created = models.DateTimeField(default=datetime.datetime.now)
  updated = models.DateTimeField(default=datetime.datetime.now)

  def __unicode__(self):
    return self.name

  @models.permalink
  def get_absolute_url(self):
    return ('cat_detail', [], {'id': self.id})

search_indexes.py

class CatIndex(indexes.BasicSearchIndex, indexes.Indexable):

  def get_model(self):
    return Cat

2 个答案:

答案 0 :(得分:3)

对于任何正在努力解决这个问题的人来说,这都归功于Django 1.9中的一点变化,其中get_model()方法被移动到django.apps.apps模块,并且django.db模块不再可用1}}

现在Haystack社区已经解决了这个问题,因此从他们的GitHub存储库(而不是PyPI)更新到最新版本必须解决它。

以下内容应解决它:

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

或者你可以像Sam建议的那样简单地降级到Django 1.8。 您可以在此问题here

上找到更多相关信息

答案 1 :(得分:0)

解决方案:将Django从1.9降级到1.8

sudo pip install Django==1.8

此处的动机link