← Django Foundations
11 / 25
Lesson 11 — QuerySet chaining
Filter step by step
Django lets you chain steps: User.objects.filter(active=True).filter(age__gte=18).order_by("n") (__gte = greater than or equal). Each step takes the previous result and narrows or sorts it.
We model each step as one line of plain Python, so you can see the data shrink:
step1 = [u for u in users if u["active"]]
Note: the exercise runs in your browser without Django installed, so it is a small plain-Python simulation of what Django does for you.
Why it matters: splitting a query into small steps makes it easy to read and to debug.
Want it another way?
Task
Add the second step (age 18 or more) and the third (sort by name), then print the names.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.