django order_by query set, ascending and descending django order_by query set, ascending and descending python python

django order_by query set, ascending and descending


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

Notice the - before check_in.

Django Documentation


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


You can also use the following instruction:

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


matomo