For example, I have 2 querysets:
q1=MyModel.objects.filter(visible=True)
q2=MyModel.objects.filter(published=True)
And i need to make single queryset or list having all objects from q1 and q2.
I used to do it like this:
q=list(chain(q1,q2))
#q= q1 | q2 #this gives me not all objects from q2 or q1,so i don't use bitwise or
But it was said, that list()
will produce extra queries to database. Is it true? And if it is, can somebody specify the most optimized way to do the merge?
q = MyModel.objects.filter(Q(visible=True) | Q(published=True))
. This will be the union of both querysets.list()
doesn't produce extra queries but forces the queryset to be evaluated directly which will lead to extra overhead in memory. – Henrik Andersson