Haystack 说“无法为 SearchResult 找到模型" [英] Haystack says “Model could not be found for SearchResult”
问题描述
将我的 Django 从 1.7 更新到 1.9 后,基于 Haystack 和 Solr 的搜索引擎停止工作.这就是我得到的:
After updating my Django from 1.7 to 1.9, search engine, which is based on Haystack and Solr, stopped working. This is what I get:
./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'u06a9u0627u0645u0631u0627u0646 u0647u0645u062au200cu067eu0648u0631 u0648 u0641u0631u0647u0627u062f u0628u0627u062fu067eu0627
Kamran 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')>'.
不得不说我的数据库不是空的,我的配置如下:
I have to say my database is not empy and my configuration is as follow:
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
推荐答案
通过包含对 2.4.1 版本的缺失提交,我能够解决该问题.解决此问题的提交是 https://github.com/django-haystack/django-haystack/commit/f1ed18313777005dd77ed724ecbfb27c0b03cad8
I was able to fix the issue by including a missing commit to the 2.4.1 release. The commit that fixed this issue was https://github.com/django-haystack/django-haystack/commit/f1ed18313777005dd77ed724ecbfb27c0b03cad8
所以你可以这样做
pip install git+ssh://git@github.com/django-haystack/django-haystack.git@f1ed18313777005dd77ed724ecbfb27c0b03cad8
安装直到该特定提交.
to install until that specific commit.
这篇关于Haystack 说“无法为 SearchResult 找到模型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!