So in my Django project, I’ve created a Notification model that has two datetime fields, one is a timestamp and one is a due date. The due date is optional. Right now I have a list of notifications, some have a due date and some don’t. They all have a timestamp. What I’m trying to do is to sort them in a list using due date for the notifications that have that field and the timestamp for the notifications that don’t have that optional field. As you can see, it’s kind of weird that I’m using two fields as the key, however they are both datetime fields. So I think they should be able to compare.
my code:
class Notifications(models.Model):
date = models.DateTimeField(auto_now=True)
title = ...
content = ...
due_date = models.DateTimeField(null=True, blank=True)
You want to use an SQL
COALESCE:I don’t think there is a django aggregate function for it, but you can do: