我有2个不同模型的2个查询集,如下所示:
qs1=[[username1, lastname1, firstname1], [username2, lastname2, firstname2]........[username10, lastname10, firstname10]]
qs2=[[username1, email1], [username2, email2]...[username10, email10]]
我需要将它们合并为一个并按用户名排序。所以它变成了这样:
qs=[[username1, lastname1, firstname1, email1], [username2, lastname2, firstname2, email2]...[username10, lastname10, firstname10, email10]]
qs1和qs2是具有多个条目的查询集。
我在view.py中有代码:
usernames = C.objects.values('username')
for username in usernames:
try:
qs1=A.objects.filter(username=username).values('username','lastname',' 'firstname')
qs2=B.objects.filter(username=username).values('username','email')
qs = sorted(chain(qs1, qs2))
return qs
如何将qs1和qs2组合到qs?
答案 0 :(得分:2)
使用itertools
from itertools import chain
result_list = list(chain(qs1, qs2))