Django order_by query set

Last updated 5 months, 3 weeks ago | 266 views 75     5

Tags:- Django

Django | order_by query set, ascending and descending

order_by()

The order_by function is used to sort the result-set in ascending or descending order.


Ascending Order

The order_by function sorts the records in ascending order by default.

Customer.objects.all().order_by('customer_id')
Customer.objects.filter(city="London").order_by('customer_id')

Descending Order

To sort the records in descending order, use a hyphen "-" in front of "check_in".

Customer.objects.all().order_by('-customer_id')
Customer.objects.filter(city="London").order_by('-customer_id')