← Back to Django Course | Chapter 6: Advanced ORM Queries | Lesson 2 of 9

Ordering QuerySets

Ordering a queryset is like lining up toys by size instead of leaving them scattered — you decide what order the results come back in.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Forgetting that querysets have no guaranteed order unless order_by() is called, then being surprised when results randomly shuffle.
  2. Mixing up ascending and descending by forgetting the leading minus sign for descending order.
  3. Ordering by a field that doesn't exist on the model, which raises a FieldError instead of silently ignoring it.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.