Django-通过排序将两个查询集合并 [英] Django - merge two querysets by sorting them
本文介绍了Django-通过排序将两个查询集合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个模型,我正在像这样查询它们:
i have two models and i am querying them like this:
firstq = FirstModel.objects.order_by('-addedtime')
secondq = SecondModel.objects.order_by('-addedtime')
我要并按 appededtime
字段对它们进行排序。他们俩都有此字段。
I want to merge and sort them by addedtime
field. they both have this field..
我该怎么做?
|
仅在我有2个来自相同模型的查询时有效。但是我这里有2种不同的模型。
|
works only if i have 2 queries from same model. But i have 2 different models here..
推荐答案
您执行的操作如下:
from itertools import chain
result_list = list(chain(firstq, secondq))
这篇关于Django-通过排序将两个查询集合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文