Skip to main content

Module queryset

Module queryset 

Source
Expand description

QuerySet: a lazy, immutable query over one model.

Builder methods consume and return the queryset and never perform I/O; terminal methods (all, first, get, count, exists, update, …) run the compiled QueryPlan on the queryset’s Db. Clone a queryset to branch from a common base.

Mistakes that can only be detected while building (an unknown annotation name, a filter on a window function) are remembered and returned by the next terminal method, so builder chains stay infallible.

§Annotations

annotate and alias name an expression; later filter / order_by / project calls refer to it with Expr::col("name") and the expression is inlined (PostgreSQL does not allow output aliases in WHERE / HAVING). A filter on an aggregate annotation becomes HAVING; when the projection contains aggregates the remaining projected columns become the GROUP BY and the model’s default ordering is dropped, as in Django.

Structs§

CursorPage
One page of a QuerySet::cursor_page result.
Page
One page of a QuerySet::paginate result.
Prefetch
prefetch_related request: a relation and, optionally, the queryset used to load the targets (to filter them, order them or use another handle).
QuerySet
Lazy query over model M.
Relation
A foreign key of model M pointing at T, together with access to its slot in M, so loaders can store the related object in the struct: