Haystack 说“找不到 SearchResult 的模型”

Haystack says “Model could not be found for SearchResult”

将我的 Django 从 1.7 更新到 1.9 后,基于 Haystack 和 Solr 的搜索引擎停止工作。这是我得到的:

./manage.py shell
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from haystack.query import SearchQuerySet
>>> sqs = SearchQuerySet().all()
>>>sqs[0].pk
u'1'
>>> sqs[0].text
u'\u06a9\u0627\u0645\u0631\u0627\u0646 \u0647\u0645\u062a\u200c\u067e\u0648\u0631 \u0648 \u0641\u0631\u0647\u0627\u062f \u0628\u0627\u062f\u067e\u0627\nKamran Hematpour & Farhad Badpa'
>>> sqs[0].model_name
u'artist'
>>> sqs[0].id
u'mediainfo.artist.1'
>>> sqs[0].object
Model could not be found for SearchResult '<SearchResult: mediainfo.artist (pk=u'1')>'.

我不得不说我的数据库不是空的,我的配置如下:

HAYSTACK_CONNECTIONS ={
    'default': {
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://ahangsolr:8983/solr',
    },
}

这是我的 search_indexes.py:

import datetime
from haystack import indexes
from mediainfo.models import Album
from mediainfo.models import Artist
from mediainfo.models import PlayList
from mediainfo.models import Track
from mediainfo.models import Lyric

class AlbumIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    artist = indexes.CharField(model_attr='artist', indexed=True)
    publish_date = indexes.DateTimeField(model_attr='publish_date')

    def get_model(self):
        return Album

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(publish_date__lte=datetime.datetime.now())


class ArtistIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return Artist


class PlaylistIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return PlayList


class TrackIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return Track


class LyricIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return Lyric

最好开始检查应用注册表以确保它可以找到您的模型(只是为了确定)。

from django.apps import apps as django_apps
model = django_apps.get_model('mediainfo.artist')
model.app_label, model.model_name
assert model._meta.app_label == 'mediainfo'
assert model._meta.model_name == 'artist'

然后我会检查 haystack 是什么 returning。

from haystack.utils.app_loading import haystack_get_model
haystack_model = haystack_get_model('mediainfo', 'artist')
haystack_model == model

如果那不是 return 相同的东西 (haystack_model != model);那么你将需要进一步挖掘。

但是,加载和查找模型在 django 1.7.0 和 1.8.0(弃用)之间发生了变化,django.db.models.loading.get_model 在 1.8.2 中被删除。 django-haystack #1206.

详情

因此,要使 django-haystackdjango 1.9.0 一起使用,您需要一个包含 this commit 的版本;也就是说 django-haystack>=2.4.0.

我能够通过包含对 2.4.1 版本的缺失提交来解决问题。修复此问题的提交是 https://github.com/django-haystack/django-haystack/commit/f1ed18313777005dd77ed724ecbfb27c0b03cad8

所以你可以

pip install git+ssh://git@github.com/django-haystack/django-haystack.git@f1ed18313777005dd77ed724ecbfb27c0b03cad8

安装直到特定提交。