QuerySet (Django)
A QuerySet is Django's representation of a database query as a collection of model objects. It is lazy — no SQL runs until you iterate, slice, or evaluate it — and chainable, so filters build up a single query.
Django
QuerySets are what the ORM returns. Post.objects.filter(published=True) gives a QuerySet, and you can keep refining it: .filter(...).exclude(...).order_by(...). Crucially it is lazy, so chaining many filters still results in one SQL query, executed only when you actually need the data.
Evaluation happens when you iterate in a loop, call list() on it, slice with an index, or call methods like .count(), .exists(), .first(), or .get(). .get() returns exactly one object or raises DoesNotExist/MultipleObjectsReturned, while .filter() returns a (possibly empty) QuerySet.
QuerySets cache their results after the first evaluation, so re-iterating the same QuerySet does not re-hit the database — but creating a new sliced or filtered QuerySet does. Understanding laziness and caching is key to writing efficient Django data code.
Syntax
Post.objects.filter(...).exclude(...).order_by(...)Examples
Python
qs = Post.objects.filter(published=True) # no query yet
qs = qs.exclude(title="") # still no query
qs = qs.order_by("-created") # still none
for post in qs: # query runs here
print(post.title)