[英]How to append multiple filter() queries into one variable
If I have three different queries of a model how do i append them into one variable 如果我对模型有三个不同的查询,如何将它们附加到一个变量中
x = AnswerModel.objects.filter(user= 'tim')
y = AnswerModel.objects.filter(user= 'paul')
z = AnswerModel.objects.filter(question= 'i like jam')
x = x.append(y)
x = x.append(z)
Use |
使用|
: :
x = AnswerModel.objects.filter(user= 'tim')
y = AnswerModel.objects.filter(user= 'paul')
z = AnswerModel.objects.filter(question= 'i like jam')
qs = x | y | z
Or, using django.db.models.Q
: 或者,使用django.db.models.Q
:
x = AnswerModel.objects.filter(Q(user='tim') | Q(user='paul') | Q(question='i like jam')
Both methods will return all results from all querysets in a single queryset. 这两种方法都将在单个查询集中返回所有查询集中的所有结果。
You need chain
. 您需要chain
。
from itertools import chain
x = list(chain(x, y, z))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.