django order_by query set, ascending and descending

前端 未结 11 2056
攒了一身酷
攒了一身酷 2020-12-07 08:59

How can I order by descending my query set in django by date?

Reserved.objects.all().filter(client=client_id).order_by(\'check_in\')

I just

11条回答
  •  自闭症患者
    2020-12-07 09:14

    If for some reason you have null values you can use the F function like this:

    from django.db.models import F

    Reserved.objects.all().filter(client=client_id).order_by(F('check_in').desc(nulls_last=True))

    So it will put last the null values. Documentation by Django: https://docs.djangoproject.com/en/stable/ref/models/expressions/#using-f-to-sort-null-values

提交回复
热议问题