Views API#
- class fastapi_restly.views.AsyncRestView#
Bases:
BaseRestViewAsyncRestView creates an async CRUD/REST interface for database objects. Basic usage:
class FooView(AsyncRestView): prefix = "/foo" schema = FooSchema model = Foo
Where
Foois a SQLAlchemy model andFooSchemaa Pydantic model.- async count_index(query_params: Any) int#
- async delete(id: Any) Response#
- async delete_object(obj: DeclarativeBase) None#
Handle a DELETE request on “/{id}”. This should delete an object from the database. on_get() is called first to lookup the object. Feel free to override this method.
- async get(id: Any) Any#
- async index(query_params: Any) Any#
- async on_create(schema_obj: BaseSchema) Any#
Handle a POST request on “/”. This should create a new object. Feel free to override this method.
- async on_delete(id: Any) Response#
- async on_get(id: Any) Any#
Handle a GET request on “/{id}”. This should return a single object. Return a 404 if not found. Feel free to override this method.
- async on_list(query_params: Any, query: Select | None = None) Sequence[Any]#
Handle a GET request on “/”. This should return a list of objects. Accepts a query argument that can be used for narrowing down the selection. Feel free to override this method, e.g.:
- async def on_list(self, query_params, query=None):
query = make_my_query() objs = await super().on_list(query_params, query) return add_my_info(objs)
- async on_update(id: Any, schema_obj: BaseSchema) Any#
Handle a PATCH request on “/{id}”. This should partially update an existing object. Feel free to override this method.
- async patch(id: Any, schema_obj: BaseSchema) Any#
- async post(schema_obj: BaseSchema) Any#
- session: Annotated[AsyncSession, Depends(async_generate_session)]#
- class fastapi_restly.views.BaseRestView#
Bases:
ViewBase class for RestView implementations.
This class contains the common functionality shared between AsyncRestView and RestView, including schema definitions, model configuration, and common CRUD operation logic.
- classmethod before_include_view()#
Apply type annotations needed for FastAPI, before creating an APIRouter from this view and registering it.
This function can be overridden to further tweak the endpoints before they are added to FastAPI.
- creation_schema: ClassVar[type[BaseSchema]]#
- exclude_routes: ClassVar[tuple[str, ...]] = ()#
- get_query_modifier_version() QueryModifierVersion#
- get_relationship_loader_options() list[Any]#
- id_type#
alias of
int
- include_pagination_metadata: ClassVar[bool] = False#
- model: ClassVar[type[DeclarativeBase]]#
- query_modifier_version: ClassVar[QueryModifierVersion]#
- request: Request#
- schema: ClassVar[type[BaseSchema]]#
- to_response_schema(obj: Any) BaseSchema#
Serialize an ORM object to the configured response schema.
- update_schema: ClassVar[type[BaseSchema]]#
- class fastapi_restly.views.RestView#
Bases:
BaseRestViewRestView creates a synchronous CRUD/REST interface for database objects. Basic usage:
class FooView(RestView): prefix = "/foo" schema = FooSchema model = Foo
Where
Foois a SQLAlchemy model andFooSchemaa Pydantic model.- count_index(query_params: Any) int#
- delete(id: Any) Response#
- delete_object(obj: DeclarativeBase) None#
Delete an object from the database. Feel free to override this method.
- get(id: Any) Any#
- index(query_params: Any) Any#
- on_create(schema_obj: BaseSchema) Any#
Handle a POST request on “/”. This should create a new object. Feel free to override this method.
- on_delete(id: Any) Response#
- on_get(id: Any) Any#
Handle a GET request on “/{id}”. This should return a single object. Return a 404 if not found. Feel free to override this method.
- on_list(query_params: Any, query: Select | None = None) Sequence[Any]#
Handle a GET request on “/”. This should return a list of objects. Accepts a query argument that can be used for narrowing down the selection. Feel free to override this method, e.g.:
- def on_list(self, query_params, query=None):
query = make_my_query() objs = super().on_list(query_params, query) return add_my_info(objs)
- on_update(id: Any, schema_obj: BaseSchema) Any#
Handle a PATCH request on “/{id}”. This should partially update an existing object. Feel free to override this method.
- patch(id: Any, schema_obj: BaseSchema) Any#
- post(schema_obj: BaseSchema) Any#
- session: Annotated[Session, Depends(generate_session)]#
- class fastapi_restly.views.View#
Bases:
objectA View that combined with include_view() will produce class-based views. Almost exactly like the @cbv decorator from fastapi-utils: https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/
- classmethod add_to_router(parent_router: APIRouter | FastAPI) None#
- classmethod before_include_view()#
- dependencies: ClassVar[list[Any] | None] = None#
- prefix: ClassVar[str]#
- responses: ClassVar[dict[int, Any]] = {404: {'description': 'Not found'}}#
- tags: ClassVar[list[str] | None] = None#
- fastapi_restly.views.delete(path: str, **api_route_kwargs: Any) Callable[[...], Any]#
Decorator to mark a View method as a DELETE endpoint.
Equivalent to:
@route(path, methods=["DELETE"], status_code=204, ... )
- fastapi_restly.views.get(path: str, **api_route_kwargs: Any) Callable[[...], Any]#
Decorator to mark a View method as a GET endpoint.
Equivalent to:
@route(path, methods=["GET"], status_code=200, ... )
- fastapi_restly.views.include_view(parent_router: APIRouter | FastAPI, view_cls: V | None = None) V | Callable[[V], V]#
Add the routes of a View class to a FastAPI app or APIRouter. This function should be used for every View class.
Can be used as a decorator:
@include_view(app) class MyView(AsyncRestView): ...
Or as a function:
include_view(app, MyView)
- fastapi_restly.views.patch(path: str, **api_route_kwargs: Any) Callable[[...], Any]#
Decorator to mark a View method as a PATCH endpoint.
Equivalent to:
@route(path, methods=["PATCH"], ... )
No default status code is set; FastAPI will use 200 if none is specified.
- fastapi_restly.views.post(path: str, **api_route_kwargs: Any) Callable[[...], Any]#
Decorator to mark a View method as a POST endpoint.
Equivalent to:
@route(path, methods=["POST"], status_code=201, ... )
- fastapi_restly.views.put(path: str, **api_route_kwargs: Any) Callable[[...], Any]#
Decorator to mark a View method as a PUT endpoint.
Equivalent to:
@route(path, methods=["PUT"], ... )
No default status code is set; FastAPI will use 200 if none is specified.
- fastapi_restly.views.route(path: str, **api_route_kwargs: Any) Callable[[...], Any]#
Decorator to mark a View method as an endpoint. The path and api_route_kwargs are passed into APIRouter.add_api_route(), see for example: https://fastapi.tiangolo.com/reference/apirouter/#fastapi.APIRouter.get
Endpoints methods are later added as routes to the FastAPI app using include_view()