merge two django querysets without changing the order

Viewed 108

I need to do two different filtering to the queryset. qs1 = qs.filter(name=value) qs2 = qs.filter(equipment_set__name=value)

Then I need to connect them without changing the order, just like they were created.

qs_result = <QuerySet [<qs1 >, <qs2>,]

1 Answers

You can work with .union(…) [Django-doc]:

qs_result = qs1.union(qs2, all=True)

This can yield the same item multiple times. To prevent that, you remove all=True:

qs_result = qs1.union(qs2)  # without duplicates
Related