Last Updated : 22 May, 2025
order_by() method in Django QuerySets is used to sort query results based on one or more fields, either in ascending or descending order. This helps display data sorted by criteria like salary, name, date, etc., directly from the database query.
In this article we will learn all about order_by method of QuesrySets by performing multiple operations over a dataset, for this we need to first create a model
Creating Model: EmployeeDetailsAssuming you have the following model defined in models.py:
Python
from django.db import models
class EmployeeDetails(models.Model):
EmployeeId = models.AutoField(primary_key=True)
EmployeeName = models.CharField(max_length=20)
EmployeeDepartment = models.CharField(max_length=20, blank=True, null=True)
Country = models.CharField(max_length=20, blank=True, null=True)
Salary = models.IntegerField(blank=True)
def __str__(self):
return self.EmployeeName
Make sure you have migrated this model:
python manage.py makemigrations
python manage.py migrate
Add some sample data via the Django admin or shell before testing.
Snapshot of Database after adding some dummy entries Using order_by() in the Django ShellOpen the Django shell:
1. Sort Employees in Ascending Order by Salary Pythonpython manage.py shell
from yourapp.models import EmployeeDetails
# Retrieve all employees ordered by Salary (lowest first)
employees = EmployeeDetails.objects.all().order_by('Salary')
for emp in employees:
print(emp.EmployeeName, emp.Salary)
This will display employees starting from the lowest salary:
Snapshot of shell 2. Sort Employees by Salary in Descending Order Python
# Using '-' prefix to sort descending by Salary
employees = EmployeeDetails.objects.all().order_by('-Salary')
for emp in employees:
print(emp.EmployeeName, emp.Salary)
The '-' before the field name reverses the order, so the highest salaries appear first:
Snapshot of shell 3. Alternative Descending Order Approaches (Not Recommended) Python
# Approach 1: Reverse the QuerySet after ascending order
employees = EmployeeDetails.objects.all().order_by('Salary').reverse()
# Approach 2: Using Python list slicing (less efficient)
employees = list(EmployeeDetails.objects.all().order_by('Salary'))[::-1]
for emp in employees:
print(emp.EmployeeName, emp.Salary)
While these work, using '-Salary' in order_by() is faster and preferred:
Snapshot of shell 4. Sort by Multiple Fields Python
# Order first by Salary ascending, then by EmployeeName ascending
employees = EmployeeDetails.objects.all().order_by('Salary', 'EmployeeName')
for emp in employees:
print(emp.EmployeeName, emp.Salary)
Employees are sorted by salary, when salaries are equal, they are sorted alphabetically by name:
Snapshot of shellUnfortunately, in the above example, our database have no entries with same salaries and thats why the output looks like a regular salary based sorted list
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4