我使用Haystack和solr对多个模型进行全局搜索,我试图对模型使用不同的过滤器,但最后我必须返回1个查询集,我不知道如何合并。
#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')
感谢
我已经找到了修改上下文的方法,但我不确定这是最好的。。。
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
也许是最好的解决方案?