6️⃣ 📊 Django QuerySets
Estimated reading: 3 minutes 288 views

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

LookupDescriptionExample
exactMatches exactlyfilter(status__exact="draft")
iexactCase-insensitive exact matchfilter(title__iexact="hello")
containsSubstring match (case-sensitive)filter(content__contains="Django")
icontainsSubstring match (case-insensitive)filter(content__icontains="django")
startswithStarts withfilter(title__startswith="How")
endswithEnds withfilter(title__endswith="Guide")
inMatches any in a listfilter(id__in=[1, 2, 3])
gt / gteGreater than / Greater than or equalfilter(price__gt=100)
lt / lteLess than / Less than or equalfilter(price__lte=50)
isnullCheck for nullfilter(description__isnull=True)
dateDate-specific filteringfilter(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 :
Share

Django QuerySet Filter

Or Copy Link

CONTENTS
Scroll to Top