Haystack objects in one query

Actually the best way to handle this is to use built-in http://docs.haystacksearch.org/dev/searchqueryset_api.html#load-all

I just missed it while checking documentation and wrote this crappy snippet!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

defhaystack_monkey_load(sqs):""" it loads all Items for given SearchQuerySet and attaches them as "object" in each SearchResult """model_dict={'app.item':Item,'another_app.comment':Comment}formodel_key,modelinmodel_dict.items():items_id=[sr.pkforsrinsqsifsr.app_label+"."+sr.model_name==model_key]items=model.objects.filter(id__in=items_id).select_related()foriteminitems:forsrinsqs:ifitem.id==sr.pkandsr.app_label+"."+sr.model_name==model_key:sr.object=itemreturnsqsclassSpecialSearchView(SearchView):defbuild_page(self):""" for example special SearchView that uses haystack_monkey_load for pagination """paginator=paginator(self.results,self.results_per_page)try:page=paginator.page(self.request.GET.get('page',1))page.object_list=haystack_monkey_load(page.object_list)exceptInvalidPage:raiseHttp404return(paginator,page)