How to return only indexed objects of a specific type in Haystack

╄→гoц情女王★ 提交于 2019-12-11 12:41:41

问题


Is there any way to use SearchQuerySet and restrict the results to only a specific indexed model? i.e. If i add Note and NoteIndex to Haystack, can I pull out just results that correspond to Note instances?

EDIT:

I have had a look and found that there is a reserved field named django_ct that is stored on every indexed model. Is it possible to filter on this field? What values does it take?

DOUBLE EDIT:

Nevermind. After reading the Haystack source code, django_ct is 'appname.modelname' internally and can be querired with SearchQuerySet.filter(django_ct = 'appname.modelname')


回答1:


According to the Haystack documentation, a SearchQueryset object has a method called models() that restricts the results to those models.

e.g.

SearchQuerySet().models(BlogEntry, Comment).filter(content='foo')

As you see, it uses the actual model class. My guess is that it uses this to lookup the content-type to perform filter.



来源:https://stackoverflow.com/questions/9466012/how-to-return-only-indexed-objects-of-a-specific-type-in-haystack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!