[Django]-Join Multiple Querysets From Different Base Models Django

38👍

from itertools import chain
report = chain(ledger, journal)

Itertools for the win!

If you want to do an Union, you should convert these querysets into python set objects.

If it is possible to filter the queryset itself rightly, you should really do that!

👤lprsd

8👍

Use itertools.chain:

from itertools import chain
report = list(chain(ledger, journal))

Note: you need to turn the resulting object into a list for Django to be able to process it.

👤Tim G

2👍

I had the same issue. I solved it using the union method combined_queryset = qs1.union(qs2)

Using your example: report = ledger.union(journal)

Leave a comment