0 day streak
python / beginner
Snippet

Filtering QuerySets

Filtering allows you to retrieve specific subsets of data from your database using keyword arguments.

snippet.py
python
1
2
3
4
5
6
7
from .models import Product
# Get all products that are active and cost more than 10
active_products = Product.objects.filter(
is_active=True,
price__gt=10
)
django
Breakdown
1
objects.filter(...)
Creates a QuerySet that contains objects matching the given criteria.
2
price__gt=10
The double underscore '__gt' stands for 'greater than'.