Quantcast
Channel: How to combine multiple querysets in Django? - Stack Overflow
Viewing all articles
Browse latest Browse all 32

Answer by Vignesh Sk for How can I combine two or more querysets in a Django view?

$
0
0

You can use Union:

qs = qs1.union(qs2, qs3)

But if you want to apply order_by on the foreign models of the combined queryset... then you need to Select them beforehand this way... otherwise it won't work.

Example

qs = qs1.union(qs2.select_related("foreignModel"), qs3.select_related("foreignModel"))qs.order_by("foreignModel__prop1")

where prop1 is a property in the foreign model.


Viewing all articles
Browse latest Browse all 32

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>