Django中合并同一个model的多个QuerySet

时间:2022-03-09 08:07:44
【1】相同model
articles1 = Article.objects.order_by("autoid").filter(autoid__lt = 16).values('autoid','title')articles2 = Article.objects.filter(autoid = 30).values('autoid','title')

articles = articles1 | articles2 # Model相同,而且没有用切片,并且字段一样时可以这样用


【2】不同model,用chain
from itertools import chain
articles1 = Article.objects.order_by("autoid").filter(autoid__lt = 16).values('autoid','title')
users = UserID.objects.all()

items = chain(articles1, users)