Search code examples
djangoisnull

django order by isnull value?


i have a datetime field which can be null and id like to do qs.order_by('field__isnull', 'name') but this results in:

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

Is this possible at all?


Solution

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

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