本文介绍了Django 1.9 / Haystack 2.4.1“SearchResult找不到模型”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先说说,我在这里尝试过修复:





基本上,Haystack 2.4.1使用过时的Django调用,这是最新的主机修复。



所以我已经用git + git://github.com/django-haystack/django-haystack.git替换了django-haystack == 2.4.1,并开始工作。 / p>

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.

解决方案

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.

这篇关于Django 1.9 / Haystack 2.4.1“SearchResult找不到模型”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:07
查看更多