Django order by isnull value?

I have a datetime field, which can be zero and id, how to do qs.order_by ('field__isnull', 'name'), but this leads to:

Join on field 'field' not permitted. Did you misspell 'isnull' for the lookup type?

Is this even possible?

+5
source share
2 answers

there may be better ways, but one possibility is to annotate your request:

from django.db.models import Count
qs.annotate(null_count=Count('field_name')).order_by('null_count')
+8
source

I hope your model looks something like this:

    date_deleted = models.DateTimeField(blank=True, null=True)

If True then you can do this:

qs.order_by('-date_deleted')
-2
source

All Articles