[Answered ]-I am trying to combine two lists of the same object, sorting with different fields but same type of data in python

2👍

You want to use an SQL COALESCE:

The COALESCE function accepts a list of parameters, returning the
first non-Null value from the list:

I don’t think there is a django aggregate function for it, but you can do:

Notifications.objects.filter(**some_filter_args)
  .extra(select={"sortdate" : 'COALESCE("due_date", "date")'})
  .order_by("-sortdate") 
👤Hamish

Leave a comment