django切片后无法合并查询 [英] django Cannot combine queries once a slice has been taken

查看:66
本文介绍了django切片后无法合并查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以连接切片的查询集?它的简化示例(我意识到可以在一个查询中编写这个特定示例):

is there any way to concatenate sliced querysets? its simplified example (i realize this particular example can be written in one query):

>>> ...
>>> query_set_1 = Model.objects.filter(...)[:3]
>>> query_set_2 = Model.objects.filter(...)[5:]

>>> query_set_1 | query_set_2

不上班,

>>> AssertionError: Cannot combine queries once a slice has been taken.

有什么建议吗?谢谢!

推荐答案

否.但是 itertools.chain() 将允许您可以依次遍历两个查询集.

No. But itertools.chain() will allow you to iterate through both querysets in order.

qiter = itertools.chain(query_set_1, query_set_2)

这篇关于django切片后无法合并查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆