Django Order By Date, but have "None" at end?
Django 1.11 added this as a native feature. It's a little convoluted. It is documented.
Ordered with only one field, ascending:
wo = Work_Order.objects.order_by(F('dateWORequired').asc(nulls_last=True))
Ordered using two fields, both descending:
wo = Work_Order.objects.order_by(F('dateWORequired').desc(nulls_last=True), F('anotherfield').desc(nulls_last=True))
q = q.extra(select={
'date_is_null': 'dateWORequired IS NULL',
},
order_by=['date_is_null','dateWORequired'],
)
You might need a - before the date_is_null in the order_by portion, but that's how you can control the behavior.