0
votes

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?

2
read carefully what i'm asking for one more time before posting comments.Feanor
Allright then 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
thanks for the list() explanation, but you also could read p.s. part of my question.Feanor

2 Answers

1
votes
q1=MyModel.objects.filter(visible=True)
q2=MyModel.objects.filter(published=True)
qs12 = QuerySetSequence(qs1, qs2)

Combine the above code with this snippet: http://djangosnippets.org/snippets/1933/

1
votes

You can try to do this:

q1_pks = MyModel.objects.filter(visible=True).values_list('pk', flat=True)
q2_pks = MyModel.objects.filter(published=True).values_list('pk', flat=True)

final_query = MyModel.objects.filter(pk__in=list(q1_pks).extend(list(q2_pks)))

That should do the trick, although i'm not sure if those list(qX_pks) produce performances issues or not.