0

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?

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Feanor
  • 3,568
  • 5
  • 29
  • 49
  • http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view – Henrik Andersson Mar 07 '13 at 13:51
  • read carefully what i'm asking for one more time before posting comments. – Feanor Mar 07 '13 at 14:02
  • 1
    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 Mar 07 '13 at 14:08
  • thanks for the list() explanation, but you also could read p.s. part of my question. – Feanor Mar 07 '13 at 15:39

2 Answers2

1
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/

catherine
  • 22,492
  • 12
  • 61
  • 85
1

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.

marianobianchi
  • 8,238
  • 1
  • 20
  • 24