Reputation: 63
I use Haystack and solr for a global search with multiple models , i try to use different filter on models, but at the end i have to return 1 queryset, i don't find how to merge this.
#views
from haystack.generic_views import SearchView
class search(SearchView):
def get_queryset(self):
queryset = super(search, self).get_queryset()
q1 = queryset.models(Event).filter(...)
q2 = queryset.models(News).filter(...)
queryset = q1 | q2 #don't work
queryset = list(chain(q1, q2)) #don't work
return queryset.order_by('-pub_date','cname')
Thanks
Upvotes: 3
Views: 264
Reputation: 63
I have find a way by modifiy the context, but im not sure this is the best...
def excludeResults(results):
for i in results:
if i.model == Event and i.date < datetime.now():
results.remove(i)
return results
class search(SearchView):
def get_queryset(self):
queryset = super(search, self).get_queryset()
return queryset.filter(visible = True).order_by('cname','-pub_date')
def get_context_data(self, *args, **kwargs):
context = super(search, self).get_context_data(*args, **kwargs)
context['page_obj'].object_list = excludeResults(context['page_obj'].object_list)
return context
maybe a best solution?
Upvotes: 1