'merge two django querysets without changing the order
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>,]
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
