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
:
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
.
from itertools import chain
x = list(chain(x, y, z))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.