is there any way to concatenate sliced querysets? its simplified example (i realize this particular example can be written in one query):
>>> ...
>>> query_set_1 = Model.objects.filter(...)[:3]
>>> query_set_2 = Model.objects.filter(...)[5:]
>>> query_set_1 | query_set_2
not going to work,
>>> AssertionError: Cannot combine queries once a slice has been taken.
any suggestions? thanks!
No. But itertools.chain()
will allow you to iterate through both querysets in order.
qiter = itertools.chain(query_set_1, query_set_2)
You can use union() to combine two sliced queryset. Like this:
query_set_1 = Model.objects.filter(...)[:3]
query_set_2 = Model.objects.filter(...)[5:]
queryset = query_set_1.union(query_set_2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With