List#
GET / returns a page of rows of the view’s model. The client can filter,
sort and page the list with query parameters, the list params; see
Filtering, Sorting & Pagination. A read changes nothing, so
it has no business method and no commit.
How a CRUD route runs explains the methods of each
route. For GET /, these steps run:
GET /get_many_endpoint(list_params)overrideThe endpoint method.handle_get_many(list_params)The handler. You call it, but you cannot override it.authorize("get_many")overrideThere is no row yet. A refusal is a 403.get_many(list_params)The load. You call it, but you cannot override it.select()The model, the scope and the loader options.apply_list_params(stmt, list_params)overrideThe filters, the sort and the page.count(stmt)overrideThe total, on a paginated view.
to_list_response(result)overrideThe list response.to_single_response(row)overrideThe response of each row.
A method marked override is a method that you can override in your view.
Visibility, then policy explains the scope and
authorize of a read.
What you can change#
The table starts with settings on the view. They need less code than an override.
Your change |
Where it goes |
|---|---|
The rows a client can see |
|
Who may list |
|
The filter and sort keys |
|
A filter of your own |
|
The size and style of a page |
|
How the list params apply |
|
The fields of each row |
|
The envelope of the list |
|
The relationships it loads |
|
A value on each row |
|
Status code, path, dependencies |
|
Parameters, headers, body |
|
No list route |
|
A subset on another path |
Details
count gives the total of a
paginated list. It counts the distinct primary keys of the rows that the
filters let through. Override it for a faster count on a large table, such
as an estimate.
Replace get_many_endpoint#
To change one option of the route, such as its dependencies, use
fr.modify_route. When the change
needs code, define get_many_endpoint in your view, with a route decorator.
Keep the call to handle_get_many, so the scope, authorize and the list
params still apply. This view sends a plain list, with the total in a
header:
from fastapi import Response
from fastapi_restly.views import ViewResponse
@fr.include_view(app)
class ProductView(fr.AsyncRestView):
prefix = "/products"
model = Product
@fr.get("/")
async def get_many_endpoint(self, list_params, response: Response) -> list[ViewResponse]:
result = await self.handle_get_many(list_params)
response.headers["X-Total-Count"] = str(result.total_count)
return result.objects
The list_params parameter keeps Restly’s filter, sort and page parameters.
The route declares a list of ViewResponse, the view’s response schema,
so Restly passes each row through to_single_response, and OpenAPI shows
the list; see The response of a custom route.
response: Response is FastAPI’s way to set a header on the response; see
Response Headers.
Replace an endpoint method has the rules for all
five routes.
Details
list_params needs no annotation: it always gets the view’s
list_params_schema.
A value from another service, or from a cache, can be added to all rows of the page with one call. The replacement calls the handler, gets the values for the whole page, and builds the response:
from fastapi_restly.views import ViewListResponse
@fr.get("/")
async def get_many_endpoint(self, list_params) -> ViewListResponse:
result = await self.handle_get_many(list_params)
ratings = await rating_service.for_ids([p.id for p in result.objects])
for project in result.objects:
project.rating = ratings.get(project.id)
return result
List rows from a custom route#
handle_get_many runs
all the steps of GET /. Call it from a route of your own to list rows
under another path. where= narrows the list inside the scope:
from fastapi_restly.views import ViewListResponse
@fr.include_view(app)
class TaskView(fr.AsyncRestView):
prefix = "/tasks"
model = Task
@fr.get("/mine")
async def mine(self, list_params) -> ViewListResponse:
return await self.handle_get_many(
list_params, where=Task.owner_id == Current.user_id()
)
GET /tasks/mine takes the same filter, sort and page parameters as
GET /tasks, and returns the same envelope. Current.user_id() is the id of
the caller; see Request Context.
List a subset inside the scope explains where=, and
Use the filters in a route of your own shows a route
that builds its own query.