问题
Let me just first say, I have tried the fixes here:
Haystack says “Model could not be found for SearchResult”
and I'm still getting
Model could not be found for SearchResult '<SearchResult: dictionary.termentry (pk=u'10')>'.
I'm on Django 1.9 & Haystack 2.4.1 with Whoosh. I've determined that the SearchQuerySet is filtering just fine (when I print queryset
I get a list of SearchResult
objects). I didn't touch anything beyond the SearchIndex definitions, so this is out-of-the-box stuff. Just for reference, here's the relevant bits of code:
in search_indexes.py:
class TermIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True,use_template=True)
rendered = indexes.CharField(use_template=True,indexed=False)
def get_model(self):
return TermEntry
def index_queryset(self,using=None):
"""Used when updating index for model"""
return self.get_model().objects.all()
in views.py:
def search(request):
query = ''
queryset = None
results = []
showresults = True
if request.method == 'GET' and request.GET.get('q'):
form = SearchForm(request.GET)
showresults = True
query = request.GET.get('q')
# making sure we got a query
print query
queryset = SearchQuerySet().filter(content=AutoQuery(query))
# checking to see if we got anything in our queryset
print queryset
for q in queryset:
print q
results.append(q.object)
# checking to see if we got any results
print results
else:
form = SearchForm()
showresults = False
context_dict = {
'query': query,
'results': results,
'form': form,
'showresults': showresults,
}
return render(request, 'search/search.html', context_dict)
I don't know what the fix is here, let alone the problem (I mean, the actual problem; I know I'm getting an error). My settings are all by-the-book, and I've nuked/rebuilt the index probably eighty times. I don't have anything funky or fancy going on with my paths or my data structure, I'm not using anything but defaults for backends and databases and such.
回答1:
I have the same problem with Solr. Found the solution here: Haystack says “Model could not be found for SearchResult”
Basically, Haystack 2.4.1 uses the outdated call to Django, this was fixed in latest the master.
So I've replaced django-haystack==2.4.1 in my requirements with git+git://github.com/django-haystack/django-haystack.git and it started to work.
来源:https://stackoverflow.com/questions/34865158/django-1-9-haystack-2-4-1-model-could-not-be-found-for-searchresult