本文介绍了过滤Django Haystack结果,例如QuerySet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将Django Haystack搜索与内置 QuerySet过滤器操作结合使用,特别是使用Q()实例和SearchQuerySet不支持的查找类型进行过滤?
Is it possible to combine a Django Haystack search with "built-in" QuerySet filter operations, specifically filtering with Q() instances and lookup types not supported by SearchQuerySet? In either order:
haystack-searched -> queryset-filtered
或
queryset-filtered -> haystack-searched
浏览Django Haystack文档没有给出如何执行此操作的指导。
Browsing the Django Haystack documentation didn't give any directions how to do this.
推荐答案
您可以使用对象的PK根据Haystack搜索的结果过滤查询集:
You could filter your queryset based on the results of a Haystack search, using the objects' PKs:
def view(request):
if request.GET.get('q'):
from haystack import ModelSearchForm
form = ModelSearchForm(request.GET, searchqueryset=None, load_all=True)
searchqueryset = form.search()
results = [ r.pk for r in searchqueryset ]
docs = Document.objects.filter(pk__in=results)
# do something with your plain old regular queryset
return render_to_response('results.html', {'documents': docs});
不确定这是如何缩放的,但是对于小的结果集(以我的情况为几百)工作正常。
Not sure how this scales, but for small resultsets (a few hundred, in my case), this works fine.
这篇关于过滤Django Haystack结果,例如QuerySet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!