[Django]-Django โ€“ merge two querysets by sorting them

1๐Ÿ‘

โœ…

You do something like:

 from itertools import chain
 result_list = list(chain(firstq, secondq))
๐Ÿ‘คAnup

4๐Ÿ‘

I think this can help you

from itertools import chain
def get_all_data():
    first = FirstModel.objects.filter()
    second = SecondModel.objects.filter()
    result_list = sorted(
        chain(first, second),
        key=lambda data: data.addedtime, reverse=True)
    return result_list
๐Ÿ‘คMr Singh

Leave a comment