7

I am trying to use Haystack and Whoosh with my Django app. I followed the steps on Haystack docs, but i am getting this error when i do a search

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()

I couldn't find help on this error anywhere, what am i doing wrong?

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
Haystack 2.4.1
Whoosh 2.7.0

doctorsherlock
  • 1,334
  • 4
  • 19
  • 41

3 Answers3

6

That looks to me like a compatibility issue between the version of Haystack and Django. Django recently reworked the get_model system (I think in 1.9), so if you've upgraded Django and not Haystack - or perhaps vice-versa - that may be the issue.

I'm guessing the get_model() references in your index file are a potential red-herring, and that the issue is within the Haystack internals, as it's not able to find that method where it expects to.

Steadman
  • 538
  • 4
  • 12
  • That seems like the issue, what should i do? – doctorsherlock Jan 04 '16 at 04:59
  • I installed Django 1.8.8 and now it is working. Must be a problem with Django 1.9 – doctorsherlock Jan 05 '16 at 07:11
  • I'd have said more likely a Haystack compatibility issue - or potentially a Whoosh compatibility issue - with Django 1.9 – Steadman Jan 06 '16 at 11:15
  • OMG if I could upvote you both 1000 times I would right now. This was the most ridiculous problem I have had in awhile. – wuno Mar 26 '16 at 04:09
  • I have the same error with Django==1.9 and Elasticsearch and the latest version of haystack (django-haystack==2.4.1):(, and downgrading to Django==1.8 I have no errors – jfunez Apr 02 '16 at 21:22
6

I replaced the following line in haystack\forms.py:

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

by this:

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]))

With the import

from django.apps import apps

I found the line to change simply by following the error callstack I was getting. For you, it seems to be in "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/forms.py"

EDIT: Getting the latest version of haystack solves the issue

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

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

Community
  • 1
  • 1
Ryan Pergent
  • 4,432
  • 3
  • 36
  • 78
0

Just define a new Method in your Search Index class (search_indexes.py)

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

     def get_model(self):
        return mymodel
Deepak Sharma
  • 1,401
  • 19
  • 21