Django ORM provides a clean and powerful way to query your database without writing raw SQL. By combining built-in filters and lookup expressions, you can efficiently retrieve exactly the data you need while keeping your code readable and maintainable.
Example Model
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length=100)
department = models.CharField(max_length=100)
salary = models.DecimalField(max_digits=10, decimal_places=2)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
filter()
Returns all records matching the given condition.
Example
employees = Employee.objects.filter(department="IT")
SQL Equivalent
SELECT * FROM employee
WHERE department = 'IT';
exclude()
Returns records that do not match the condition.
Example
employees = Employee.objects.exclude(department="HR")
SQL Equivalent
SELECT * FROM employee
WHERE department != 'HR';
get()
Returns a single object.
Example
employee = Employee.objects.get(id=1)
If no record exists or multiple records exist, Django raises an exception.
Common Built-in Lookup Filters
exact
Exact match.
Employee.objects.filter(name="Tarun")
iexact
Case-insensitive exact match.
Employee.objects.filter(name__iexact="tarun")
contains
Case-sensitive contains.
Employee.objects.filter(name__contains="aru")
icontains
Case-insensitive contains.
Employee.objects.filter(name__icontains="aru")
startswith
Checks if a field starts with a value.
Employee.objects.filter(name__startswith="Tar")
istartswith
Case-insensitive startswith.
Employee.objects.filter(name__istartswith="tar")
endswith
Checks if a field ends with a value.
Employee.objects.filter(name__endswith="Kumar")
iendswith
Case-insensitive endswith.
Employee.objects.filter(name__iendswith="kumar")
Numeric Filters
Greater Than (gt)
Employee.objects.filter(salary__gt=50000)
Greater Than or Equal (gte)
Employee.objects.filter(salary__gte=50000)
Less Than (lt)
Employee.objects.filter(salary__lt=50000)
Less Than or Equal (lte)
Employee.objects.filter(salary__lte=50000)
in Lookup
Retrieve records matching any value in a list.
Employee.objects.filter(id__in=[1, 2, 3, 4])
range Lookup
Retrieve records within a range.
Employee.objects.filter(salary__range=(30000, 80000))
Boolean Filtering
Employee.objects.filter(is_active=True)
NULL Filtering
IS NULL
Employee.objects.filter(department__isnull=True)
IS NOT NULL
Employee.objects.filter(department__isnull=False)
Date Filtering
Assume a field:
created_at = models.DateTimeField(auto_now_add=True)
Filter by Year
Employee.objects.filter(created_at__year=2026)
Filter by Month
Employee.objects.filter(created_at__month=6)
Filter by Day
Employee.objects.filter(created_at__day=24)
Multiple Conditions
AND Condition
Employee.objects.filter(
department="IT",
is_active=True
)
OR Condition
from django.db.models import Q
Employee.objects.filter(
Q(department="IT") |
Q(department="HR")
)
Ordering Data
Ascending
Employee.objects.order_by("salary")
Descending
Employee.objects.order_by("-salary")
Limiting Records
First 10 Records
Employee.objects.all()[:10]
Skip First 10 Records
Employee.objects.all()[10:]
Key Takeaways
filter() → Get matching records
exclude() → Exclude matching records
get() → Retrieve a single object
Q() → Perform OR conditions
Lookup expressions such as icontains, gte, lte, range, and in provide powerful filtering options
Django ORM filters are translated directly into SQL queries, making them efficient and easy to use