[英]Merge multiple SearchQuerySet
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. 我使用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')
Thanks 谢谢
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? 也许是最佳解决方案?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.