1👍
✅
Use the pipe to merge querysets:
combined = qs1 | qs2 | qs3
But this wont work for querysets from distinct models, at least not if they have diffferent fields. You will get an Cannot combine queries on two different base models.
error.
A possible solution is to use itertools.chain
, like in this blog post, which i quote here for reference.
from itertools import chain
result_lst = list(chain(queryset1, queryset2))
Now, you can sort the resulting list by any common field, e.g. creation date
from operator import attrgetter
result_lst = sorted(
chain(queryset1, queryset2),
key=attrgetter('created_at'))
Source:stackexchange.com