As a Python developer with over a decade of experience, I have worked extensively with Django’s ORM to build scalable and efficient web applications. One of the most common tasks I encounter is ordering query results. Whether you want to display the latest blog posts first or sort user data alphabetically, knowing how to order your queries in ascending or descending order is essential.
In this tutorial, I’ll walk you through simple yet effective ways to order querysets in Django. I’ll share practical examples that you can apply directly to your projects, especially if you’re working with data relevant to the US market, such as sorting customer records, sales data, or event logs.
Let’s dive straight into it!
1. Order Query Results Using Django ORM
Django’s ORM (Object-Relational Mapping) provides a very intuitive way to interact with your database. Ordering querysets is straightforward with the .order_by() method.
Basic Usage of .order_by()
The .order_by() method allows you to specify one or more fields by which to sort your query results.
- To order in ascending order, simply pass the field name.
- To order in descending order, prefix the field name with a minus
-.
Example: Ordering Customers by Last Name
Imagine you have a Customer model with fields like first_name, last_name, and signup_date. You want to list customers ordered alphabetically by their last name.
from myapp.models import Customer
# Ascending order by last_name
customers_asc = Customer.objects.all().order_by('last_name')
for customer in customers_asc:
print(customer.last_name, customer.first_name)If you want to reverse the order and show customers starting from Z to A:
# Descending order by last_name
customers_desc = Customer.objects.all().order_by('-last_name')
for customer in customers_desc:
print(customer.last_name, customer.first_name)I executed the above example code and added the screenshot below.

2. Order by Multiple Fields
Sometimes, you may want to order by more than one field. For example, if two customers have the same last name, you may want to order them by their first name as well.
# Order by last_name ascending, then first_name ascending
customers = Customer.objects.all().order_by('last_name', 'first_name')Or to order by last name ascending but first name descending:
# Order by last_name ascending, first_name descending
customers = Customer.objects.all().order_by('last_name', '-first_name')I executed the above example code and added the screenshot below.

Read Union Operation on Django Models
3. Order Query Results by Date Fields
Sorting by date is a common requirement, especially in US-based applications like event management or sales tracking.
Example: Ordering Events by Date
Suppose you have an Event model with a date field. To get upcoming events first (ascending order):
from myapp.models import Event
upcoming_events = Event.objects.filter(date__gte='2024-01-01').order_by('date')
for event in upcoming_events:
print(event.name, event.date)To show the most recent events first (descending order):
recent_events = Event.objects.filter(date__lte='2024-01-01').order_by('-date')
for event in recent_events:
print(event.name, event.date)I executed the above example code and added the screenshot below.

Check out Python Filter Not in Django
4. Use .reverse() Method to change the Order
If you already have a queryset and want to reverse its order without specifying the fields again, Django provides the .reverse() method.
customers = Customer.objects.all().order_by('last_name')
customers_reversed = customers.reverse()
for customer in customers_reversed:
print(customer.last_name, customer.first_name)Note: .reverse() only works on querysets that have an ordering applied.
5. Ordering QuerySets with Raw SQL
While Django ORM covers most use cases, sometimes you might need raw SQL queries for complex ordering logic.
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM myapp_customer ORDER BY last_name DESC")
rows = cursor.fetchall()
for row in rows:
print(row)However, I recommend using Django ORM’s .order_by() whenever possible for maintainability and security.
Read Check if Python Dictionary is Empty
Real-World Example: Sort US Customers by State and Signup Date
Let’s say you manage a US-based e-commerce platform, and you want to list customers first by their state (alphabetically) and then by their signup date (newest first).
customers = Customer.objects.all().order_by('state', '-signup_date')
for customer in customers:
print(customer.state, customer.signup_date, customer.first_name)This ordering helps customer support prioritize recent customers in each state.
Ordering query results in Django is simple and powerful with the .order_by() method. Whether you want ascending or descending order, single or multiple fields, Django ORM has you covered.
Remember these quick tips:
- Use
.order_by('field')for ascending order. - Use
.order_by('-field')for descending order. - Chain multiple fields in
.order_by()for complex sorting. - Use
.reverse()to invert the order of an existing queryset. - Use raw SQL only for advanced cases where ORM falls short.
By mastering these techniques, you can efficiently handle data sorting in your Django applications and provide better user experiences.
I hope this article helps you master ordering queries in Django. Happy coding!
Other Python Django articles you may also like:
- Compare Two Integers in Python Django
- Payment Gateway Integration with Django
- How to Add Items to Cart in Django in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.