BeginnerCoder

Lesson 4 — Filtering data

Asking for some of the records

A QuerySet is Django's list of records from the database. You narrow it with .filter(): Product.objects.filter(price__lt=15) means "products with price less than 15" (__lt = less than).

In plain Python the same idea is a list comprehension — a one-line loop that keeps only matching items:

adults = [u for u in users if u["age"] >= 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: almost every page shows some of the data — today's orders, this user's posts — so filtering is everywhere.

Want it another way?
Task

Keep only products cheaper than 15 and print their names, one per line.

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.