Python 干草堆说“;找不到SearchResult的模型”;
将我的Django从1.7更新到1.9后,基于Haystack和Solr的搜索引擎停止工作。这就是我得到的:Python 干草堆说“;找不到SearchResult的模型”;,python,django,nginx,solr,django-haystack,Python,Django,Nginx,Solr,Django Haystack,将我的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
./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')>'.
这是我的搜索索引.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
最好开始检查应用程序注册表,以确保它可以找到您的模型(只是为了确保) 然后我会检查haystack返回的内容
from haystack.utils.app_loading import haystack_get_model
haystack_model = haystack_get_model('mediainfo', 'artist')
haystack_model == model
如果没有返回相同的内容(haystack\u model
!=model
);然后你需要进一步挖掘
但是,加载和查找模型在django
1.7.0和1.8.0(弃用)之间发生了变化,并且django.db.models.load.get_model
在1.8.2中被删除。
有关的详细信息
因此,要使用
django-haystack
来使用django
1.9.0,您需要一个包含以下内容的版本:;也就是说,django haystack>=2.4.0我能够通过在2.4.1版本中包含一个丢失的提交来修复这个问题。解决此问题的提交是
所以你可以
pip install git+ssh://git@github.com/django-haystack/django-haystack.git@f1ed18313777005dd77ed724ecbfb27c0b03cad8
安装,直到该特定提交 您使用的是什么版本的Haystack?@ThomasOrozco Haystack 2.4.1我的Haystack版本是2.4.1,django_应用程序。get_模型('mediainfo.artist')返回时没有错误:。我还可以提供其他任何信息来帮助解决此问题吗?haystack_model==model返回True,但在使用模型的app_label和model_name属性时出现错误:AttributeError:type对象“Artist”没有属性“model_name”,AttributeError:type对象“Artist”没有属性“app_label”!!!同样面对这个问题-你得到解决了吗?对我来说,haystack_model==model和这个问题一直存在hanks@elchudi这是我的答案,但我对它做了一些修改:pip安装git+它真的很管用你很受欢迎golden_boy615,我不会在pip中跟踪@master,因为master会一直变化,在django haystack的新版本发布之前,最好只跟踪一个您知道有效的特定提交
from haystack.utils.app_loading import haystack_get_model
haystack_model = haystack_get_model('mediainfo', 'artist')
haystack_model == model
pip install git+ssh://git@github.com/django-haystack/django-haystack.git@f1ed18313777005dd77ed724ecbfb27c0b03cad8