Django-Haystack giving attribute error?

2024/10/14 17:16:30

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 Movieclass MovieIndex(indexes.SearchIndex, indexes.Indexable):text = indexes.CharField(document=True, use_template=True)title = indexes.CharField(model_attr='title')def get_model(self):return Moviedef 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.movieDjango 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_response149.                     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_response147.                     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_results91.         return self.form.search()File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/forms.py" in search116.         return sqs.models(*self.get_models())File "/home/dr_sherlock/movienalyse/virmovienalyse/local/lib/python2.7/site-packages/haystack/forms.py" in get_models110.                 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

Answer

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.

https://en.xdnf.cn/q/69385.html

Related Q&A

python calendar with holidays [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:Holiday Calendars, File Formats, et al. Hi, Is there a calendar library in Python with which I can check for holidays, com…

How to choose your conda environment in Jupyter Notebook

I installed Anaconda 5.3 with Python 3.7 (root environment). After that I created a new environment (py36) using Python 3.6I activated the new environment with activate py36 conda env list shows that t…

How do I stagger or offset x-axis labels in Matplotlib?

I was wondering if there is an easy way to offset x-axis labels in a way similar to the attached image.

graphviz segmentation fault

Im building a graph with many nodes, around 3000. I wrote a simple python program to do the trick with graphviz, but it gives me segmentation fault and I dont know why, if the graph is too big or if im…

how to pass char pointer as argument in ctypes python

Please help me in converting below line of c++ code into ctypes python:Ret = openFcn(&Handle, "C:\\Config.xml");below are the declarations of each:typedef uint16_t (* OpenDLLFcnP)(void **…

Restarting a Python Interpreter Quietly

I have a python interpreter embedded inside an application. The application takes a long time to start up and I have no ability to restart the interpreter without restarting the whole application. What…

Unique lists from a list

Given a list I need to return a list of lists of unique items. Im looking to see if there is a more Pythonic way than what I came up with:def unique_lists(l):m = {}for x in l:m[x] = (m[x] if m.get(x) !…

How to run Spyder with Python 3.7 with Anaconda

I have installed Anaconda on a Windows 10 machine which comes with Spyder and Python 3.6 but I wish to upgrade to Python 3.7To create an Anaconda Environment with Python 3.7 is easy by using:conda crea…

Pass in a list of possible routes to Flask?

Im learning Flask and have a question about dynamic routing: is it possible to pass in a list of accepted routes? I noticed the any converter which has potential but had a hard time finding examples o…

Tornado @run_on_executor is blocking

I would like to ask how tornado.concurrent.run_on_executor (later just run_on_executor) works, because I probably do not understand how to run synchronous task to not block the main IOLoop.All the exa…