David542
David542

Reputation: 110382

Remove NULL in desc date ordering

I have the following query:

 tasks.order_by('order_item__due_date')

However, the first results that show have due_date = NULL.

How would I do the ordering such that the due_date must first be non-null, and then order by date. Something like:

tasks.order_by('order_item__due_date=not null', 'order_item__due_date')

Note that I do not want to filter out any results. I just want to push null results to the end. The equivalent of doing this:

  tasks.filter(order_item__due_date__isnull=False).order_by('due_date)
+ tasks.filter(order_item__due_date__isnull=True)

Upvotes: 0

Views: 62

Answers (1)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53999

tasks.filter(order_item__due_date__isnull=False).order_by('order_item__due_date')

isnull QuerySet method documentation

Upvotes: 1

Related Questions