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:

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

scope

Who may list

authorize

The filter and sort keys

include_filters

A filter of your own

fr.list_filter

The size and style of a page

pagination

How the list params apply

apply_list_params

The fields of each row

list_response_schema

The envelope of the list

pagination

The relationships it loads

get_loader_options

A value on each row

to_single_response

Status code, path, dependencies

fr.modify_route

Parameters, headers, body

get_many_endpoint

No list route

get_many_endpoint = None

A subset on another path

handle_get_many

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.