Koskamo

Lazy QuerySets and their cache

Document v1 · published by Koskamo editorial · updated 2026-10-04

Original source

QuerySets are lazy: creating one involves no database activity, and you can stack filters without Django running anything until the QuerySet is evaluated. Each QuerySet also contains a cache to minimize database access. In a newly created QuerySet the cache is empty; the first time it is evaluated, and a database query happens, Django saves the results in the QuerySet's cache and returns what was explicitly requested — the next element, if you are iterating — and subsequent evaluations reuse the cached results. Creating two separate QuerySets over the same data evaluates each independently, executing the same query twice and doubling database load. For related objects, the select_related() method prepopulates the relationship cache ahead of time, so follow-up attribute access does not hit the database again. For single rows, filter() always returns a QuerySet even when only one object matches, while a Manager's get() returns the object directly. Scope: Django. Reviewed October 4, 2026.

Source and provenance

Original source
docs.djangoproject.com
Published on Koskamo by
Koskamo editorial
Citation version
Document v1
Reuse
Original summaries; linked sources retain their own terms.

The original source supplies the underlying information. Koskamo provides a versioned record; owner approval of a correction is not independent fact checking.