🔍 Django QuerySet Filter – Retrieve Multiple Records with Conditions (2025 Guide)
🧲 Introduction – What Is .filter()
in Django?
The .filter()
method in Django’s QuerySet API is used to retrieve multiple records from the database that match specific conditions. It returns a QuerySet (not just one object) and supports powerful field lookups, chaining, and dynamic filtering.
🎯 In this guide, you’ll learn:
- How to use
.filter()
to query your models - The most common field lookups (
exact
,icontains
,gte
, etc.) - How to combine multiple conditions
- Best practices for efficient filtering
🧱 Basic Syntax
Model.objects.filter(field=value)
✅ Returns a QuerySet of objects that match the condition.
🧪 Example: Filtering Posts
from blog.models import Post
# Get all published posts
posts = Post.objects.filter(status='published')
✔️ Returns a list (QuerySet) of posts where status == "published"
.
🔍 Common Field Lookups
Lookup | Description | Example |
---|---|---|
exact | Matches exactly | filter(status__exact="draft") |
iexact | Case-insensitive exact match | filter(title__iexact="hello") |
contains | Substring match (case-sensitive) | filter(content__contains="Django") |
icontains | Substring match (case-insensitive) | filter(content__icontains="django") |
startswith | Starts with | filter(title__startswith="How") |
endswith | Ends with | filter(title__endswith="Guide") |
in | Matches any in a list | filter(id__in=[1, 2, 3]) |
gt / gte | Greater than / Greater than or equal | filter(price__gt=100) |
lt / lte | Less than / Less than or equal | filter(price__lte=50) |
isnull | Check for null | filter(description__isnull=True) |
date | Date-specific filtering | filter(published__date='2025-05-23') |
🧠 Combine Multiple Conditions
# AND condition (chained filters)
Post.objects.filter(status='published', category='tutorial')
# OR condition using Q objects
from django.db.models import Q
Post.objects.filter(Q(status='draft') | Q(category='guide'))
📋 Real-World Filtering Examples
➤ Filter Posts by Author
Post.objects.filter(author__username='admin')
➤ Case-Insensitive Search
Post.objects.filter(title__icontains='django')
➤ Filter by Date Range
Post.objects.filter(published__year=2025, published__month=5)
✅ Best Practices
- ✅ Use indexing and limit filters on large datasets
- ✅ Always return QuerySets (
filter()
), not objects (get()
), unless expecting one - ✅ Chain
.filter()
for clean AND logic - ✅ Use
Q()
objects for complex OR queries
📌 Summary – Recap & Next Steps
🔍 Key Takeaways:
.filter()
retrieves all records that match a condition- Supports dozens of flexible field lookups
- Returns a QuerySet (can be sliced, ordered, etc.)
- Enables AND/OR logic using chaining and
Q()
⚙️ Real-World Relevance:
Used in nearly every Django app—blogs, e-commerce filters, dashboards, and search features all rely on .filter()
to query the database.
❓ Frequently Asked Questions (FAQ)
❓ What’s the difference between .get()
and .filter()
?
✅ .get()
returns one object and raises errors if not found.
✅ .filter()
returns a QuerySet and never raises errors.
❓ How do I filter case-insensitively?
✅ Use __iexact
or __icontains
.
❓ Can I filter using foreign key fields?
✅ Yes:
Post.objects.filter(author__email='admin@example.com')
❓ Can I combine multiple .filter()
calls?
✅ Yes:
Post.objects.filter(status='published').filter(category='python')
❓ Is .filter()
lazy?
✅ Yes. The query isn’t executed until evaluated (e.g., iterated, counted, or converted to a list).
Share Now :