Ordering QuerySets
Why Order Matters
Without order_by(), Django doesn't promise any particular order — the database can return rows in whatever order is fastest for it, which can change between calls.
Note: Always add order_by() when the display order matters to users.
Example: Why Order Matters
The first call may return chapters in any sequence; the second always returns them sorted by their order field, ascending.
from core.models import Chapter
# Order is not guaranteed
Chapter.objects.all()
# Order is now guaranteed by 'order' field
Chapter.objects.order_by('order')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Ascending and Descending Order
A plain field name like title sorts smallest/earliest first (ascending); putting a minus sign in front, like '-title', reverses it to descending.
Example: Ascending and Descending Order
The second query returns the exact same tutorials as the first, just in reverse alphabetical order.
from core.models import Tutorial
# A to Z
Tutorial.objects.order_by('title')
# Z to A
Tutorial.objects.order_by('-title')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Ordering by Multiple Fields
You can pass several field names to order_by(); Django sorts by the first field, and uses the next fields to break ties.
Note: List the most important sort key first.
Example: Ordering by Multiple Fields
Tutorials are grouped by category alphabetically, and within each category they're sorted by their order field.
from core.models import Tutorial
# Sort by category, then by order within each category
Tutorial.objects.order_by('category', 'order')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting that querysets have no guaranteed order unless order_by() is called, then being surprised when results randomly shuffle.
- Mixing up ascending and descending by forgetting the leading minus sign for descending order.
- Ordering by a field that doesn't exist on the model, which raises a FieldError instead of silently ignoring it.
- order_by() controls the order results are returned in.
- A plain field name sorts ascending; a leading - sorts descending.
- You can order by multiple fields by listing them in priority order.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: