Django: Adding “NULLS LAST” to query

前端 未结 8 2122
时光说笑
时光说笑 2020-11-27 14:25

I would like to sort a model by using Postgresql\'s \"NULLS LAST\" option. How could it be done?

I tried something like

MyModel.objects.all().extra(ord

8条回答
  •  [愿得一人]
    2020-11-27 14:37

    from django.db.models import F  
    MyModel.objects.all().order_by(F('price').desc(nulls_last=True))
    

    This functionality has been added to Django 1.11.

    https://docs.djangoproject.com/en/dev/releases/1.11/

    Added the nulls_first and nulls_last parameters to Expression.asc() and desc() to control the ordering of null values.

提交回复
热议问题