Query API#
fastapi_restly.query implements list-endpoint filtering, sorting, and
pagination: derive_schema_list_params() derives the list params, a model
of the URL parameters, from the view’s response class and its model, and
apply_list_params() applies validated list params to a SQLAlchemy select.
- fastapi_restly.query.apply_list_params(query: Select, list_params: BaseModel | QueryParams, model: type[DeclarativeBase], schema_response: type[BaseModel], *, pagination: NumberedPagination | NoPagination | None = ...) Select#
Apply pagination, sorting, and filtering to
queryusing validated list params.list_paramsis normally an instance of the model returned byderive_schema_list_params(). The default list endpoints always pass a validated instance, so pagination/filter bounds have already been checked.schema_responsedecides the fields a client can filter and sort on. A view passes its response class.The arguments match the view method
apply_list_params(), which calls this function with the view’s model, response class and pagination.A raw
QueryParamsis also accepted for callers that build the query parameters programmatically. Raw inputs bypass schema validation — the caller is responsible for verifyingpage/page_sizeranges and any per-view bounds (max_page_size); this function only performs the minimum coercion needed to apply the SQL clauses.paginationnames the page and page-size parameters to read. By default it is the pagination thatderive_schema_list_params()created the list params model with, andpageandpage_sizefor rawQueryParamsor a hand-written model. ANoPaginationorNoneapplies noLIMIT/OFFSET, and so does a missing page-size key.Examples:
# Pagination page=2&page_size=50 # Sorting sort=name,-created_at # Filtering name=Bob&status=active&created_at__gte=2024-01-01 # Contains (string fields) name__contains=John&email__icontains=example
- fastapi_restly.query.derive_schema_list_params(schema_response: type[BaseModel], model: type[DeclarativeBase], *, pagination: NumberedPagination | NoPagination | None = NumberedPagination(default_page_size=50, max_page_size=1000, max_page=None, page_query_param='page', page_size_query_param='page_size', envelope=PaginatedEnvelope)) type[BaseModel]#
Build the list params: a Pydantic model that describes and validates the URL query parameters of a list endpoint. A view keeps it in
schema_list_params.The generated model accepts pagination (
page,page_size), sorting (sort), and one filter parameter per field ofschema_responsethat maps to a filterable column onmodel– with optional__in/__ne/__gte/__lte/__gt/__lt/__isnull/__contains/__icontainssuffixes. Fields that do not resolve to a column (relationship/collection fields, or reference traversals the request path would reject) get no filter params, so the generated schema – and the OpenAPI it produces – no longer advertises filters for fields that are not filterable at all. WriteOnly fields get none either, since a filter on one would leak its value. Fields whose type is a collection (dict/list, e.g.JSONorARRAYcolumns) generate only__isnull: a query-string value cannot coerce into them, so every other operator would fail at request time.With a
pagination(the default ispageandpage_size), its two query parameters are added under its names and validated by Pydantic with bounds: the page at least 1 and at mostmax_pagewhen set, the page size from 1 tomax_page_size. The resulting SQL offset must also fit in a signed 64-bit integer. Out-of-range values produce a standard 422 response from FastAPI. With aNoPaginationorNone, no pagination parameters are emitted at all – the endpoint returns every matching row – while sorting and filtering stay available.- Parameters:
schema_response – The response class whose fields decide the available filter parameters. A view passes its
schema_response, so a client can filter only on what it can see.model – The SQLAlchemy model the list endpoint queries. Used to verify each field resolves to a filterable column; non-column fields are omitted from the generated params.
pagination – The view’s pagination settings. A
NoPaginationorNoneis an unpaginated view, which returns the full result set.
See also
Filter, Sort, and Paginate Lists documents the URL filter, sort, and pagination grammar that these helpers implement.