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")
Source:stackexchange.com