django order_by query set, ascending and descending

前端 未结 11 2069
攒了一身酷
攒了一身酷 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:15

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

    A hyphen "-" in front of "check_in" indicates descending order. Ascending order is implied.

    We don't have to add an all() before filter(). That would still work, but you only need to add all() when you want all objects from the root QuerySet.

    More on this here: https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters

提交回复
热议问题