我使用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')
由于
答案 0 :(得分:1)
我通过修改上下文找到了一种方法,但我不确定这是最好的......
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
也许是最好的解决方案?