Skip to content
Justice Tech School

QuerySets

A QuerySet represents a database query. filter and exclude return QuerySets, get returns one object, and order_by sorts. QuerySets are lazy, so SQL runs only when you use the result.

Example
Post.objects.all()
Post.objects.filter(title="Hello")
Post.objects.exclude(title="Hello")
Post.objects.get(id=1)
Post.objects.order_by("-created")[:5]
Post.objects.first()

Exercise

Newest first.

order_by("created")

Quiz Test

What does get raise if nothing matches?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.