BeginnerCoder

Lesson 15 — QuerySet filtering

A reusable filter function

Django's .filter(age__gte=18) reads like a sentence. Behind it is a simple idea: look at each record and keep it if the condition holds. Let's write our own filter_gte(rows, field, value) that keeps rows where rows[field] >= value.

filter_gte(users, "age", 18)   # like User.objects.filter(age__gte=18)

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: once you know what the ORM does underneath, its errors and results stop being a mystery.

Want it another way?
Task

Finish filter_gte, then print the names of users aged 18 or more.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.