如何在Django视图中组合2个或多个查询集?我试图建立一个Django网站的搜索,我正在建设,在搜索中,我正在搜索3种不同的模式。为了获得搜索结果列表上的分页,我想使用一个GenericObjectList视图来显示结果。但要做到这一点,我必须将3个查询合并为一个。我怎么能这么做?我试过这个:result_list = [] page_list = Page.objects.filter(
Q(title__icontains=cleaned_search_term) |
Q(body__icontains=cleaned_search_term))article_list = Article.objects.filter(
Q(title__icontains=cleaned_search_term) |
Q(body__icontains=cleaned_search_term) |
Q(tags__icontains=cleaned_search_term))post_list = Post.objects.filter(
Q(title__icontains=cleaned_search_term) |
Q(body__icontains=cleaned_search_term) |
Q(tags__icontains=cleaned_search_term))for x in page_list:
result_list.append(x)for x in article_list:
result_list.append(x)for x in post_list:
result_list.append(x)return object_list(
request,
queryset=result_list,
template_object_name='result',
paginate_by=10,
extra_context={
'search_term': search_term},
template_name="search/result_list.html")但是这不起作用,当我试图在泛型视图中使用这个列表时,我会得到一个错误。列表缺少克隆属性。谁知道我怎么把这三份名单合并,page_list, article_list和post_list?
添加回答
举报
0/150
提交
取消