I was working with a django application where I need to merge two query set. After going through django ORM docs, could not find anything helpful.
I was planning to do it in a unpythonic way like iterating two queryset and appending each item to a new list, just before doing it I thought it would be better to google for it. And after couple of minutes found it. We can use python itertools to merge two or more query set. Like below:
|from itertools import chain|
|cars = Cars.objects.all()|
|trucks = Truck.objects.all()|
|all_vechiles = list(chain(cars, trucks))|
Python itertools is an amazing module that contains real handy methods what we need to handle iterators and doing different types of operation. If you never used itertools before you are missing one of the charm of python.
Check Itertools chain docs for details.