Django Tutorial · QuerySets

QuerySet Order By

Learn all about QuerySet Order By in this comprehensive tutorial.

5 min read intermediate
  • To sort QuerySets, Django uses the order_by() method:
  • By default, the result is sorted ascending (the lowest value first), to change the direction to descending (the highest value first), use the minus sign (NOT), - in front of the field name:
  • To order by more than one field, separate the fieldnames with a comma in the order_by() method:

Order By

To sort QuerySets, Django uses the order_by() method:

In SQL, the above statement would be written like this:

Descending Order

By default, the result is sorted ascending (the lowest value first), to change the direction to descending (the highest value first), use the minus sign (NOT), - in front of the field name:

In SQL, the above statement would be written like this:

Multiple Order Bys

To order by more than one field, separate the fieldnames with a comma in the order_by() method:

In SQL, the above statement would be written like this:

Module quiz

2 questions
1

Which of the following is true about QuerySet Order By?

2

What is the most common pitfall when working with QuerySet Order By?

Answer all questions to submit.