FastAPI Class Views and utilities
Project description
fastapi-views
Class-based views, CRUD utilities, and production-ready patterns for FastAPI.
FastAPI Views brings Django REST Framework-style class-based views to FastAPI — without giving up type safety or dependency injection. Define a full CRUD resource by inheriting one class; routes, status codes, and OpenAPI docs are wired up automatically.
Features
- Class-based views —
View,APIView,ViewSet, andGenericViewSetat three levels of abstraction; mix-in only the actions you need - Full CRUD in one class —
list,create,retrieve,update,partial_update,destroywith correct HTTP semantics out of the box (201 Created,204 No Content,Locationheader, etc.) - Generic views with the repository pattern — plug in any data source (SQLAlchemy, Motor, plain dicts) via a simple protocol; no ORM dependency
- DRF-style filters —
ModelFilter,OrderingFilter,SearchFilter,PaginationFilter,TokenPaginationFilter,FieldsFilter, and a combinedFilterclass; built-in SQLAlchemy and Python object resolvers - RFC 9457 Problem Details — every error response is machine-readable; built-in classes for the most common cases; custom errors auto-register in the OpenAPI spec
- Fast Pydantic v2 serialization —
TypeAdaptercached per schema type avoids the double validation/model instantiation that FastAPI does by default, reducing per-request overhead - Server-Sent Events —
ServerSideEventsAPIViewand@sse_routehandle framing, content-type, and Pydantic validation automatically - Async and sync support — every class ships an
Asyncand a synchronous variant; sync endpoints run in a thread pool - One-call setup —
configure_app(app)registers error handlers, Prometheus middleware, and OpenTelemetry instrumentation - Prometheus metrics —
/metricsendpoint with request count, latency histogram, and in-flight requests (optional extra) - OpenTelemetry tracing —
correlation_idinjected into every error response for easy trace correlation (optional extra) - Readable OpenAPI operation IDs —
list_item,create_item,retrieve_iteminstead of FastAPI's long path-derived defaults - CLI — export a static
openapi.json/openapi.yamlwithout starting a server
Documentation: https://asynq-io.github.io/fastapi-views/
Repository: https://github.com/asynq-io/fastapi-views
Installation
pip install fastapi-views
Optional dependencies
Avaliable extensions: uvloop, prometheus, uvicorn, opentelemetry, cli
pip install 'fastapi-views[all]'
Quick start
from typing import ClassVar, Optional
from uuid import UUID
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi_views import ViewRouter, configure_app
from fastapi_views.views.viewsets import AsyncAPIViewSet
class ItemSchema(BaseModel):
id: UUID
name: str
price: int
class ItemViewSet(AsyncAPIViewSet):
api_component_name = "Item"
response_schema = ItemSchema
# In-memory store — swap for a real repository in production
items: ClassVar[dict[UUID, ItemSchema]] = {}
async def list(self) -> list[ItemSchema]:
return list(self.items.values())
async def create(self, item: ItemSchema) -> ItemSchema:
self.items[item.id] = item
return item
async def retrieve(self, id: UUID) -> Optional[ItemSchema]:
return self.items.get(id)
async def update(self, id: UUID, item: ItemSchema) -> ItemSchema:
self.items[id] = item
return item
async def destroy(self, id: UUID) -> None:
self.items.pop(id, None)
router = ViewRouter(prefix="/items")
router.register_view(ItemViewSet)
app = FastAPI(title="My API")
app.include_router(router)
configure_app(app)
This registers the following routes automatically:
| Method | Path | Action | Status code |
|---|---|---|---|
GET |
/items |
list |
200 |
POST |
/items |
create |
201 |
GET |
/items/{id} |
retrieve |
200 |
PUT |
/items/{id} |
update |
200 |
DELETE |
/items/{id} |
destroy |
204 |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_views-1.6.2.tar.gz.
File metadata
- Download URL: fastapi_views-1.6.2.tar.gz
- Upload date:
- Size: 23.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fdbcf905f52154b0fb1d5b2311504a5f82349ea42a30d4b9fc06baa6ca4c7922
|
|
| MD5 |
9b896fe21e407811154933e3553d7544
|
|
| BLAKE2b-256 |
36b87a8bab102dbf62aee9c6a19461b466b8490dab1f8df4d539d38b6f09124d
|
File details
Details for the file fastapi_views-1.6.2-py3-none-any.whl.
File metadata
- Download URL: fastapi_views-1.6.2-py3-none-any.whl
- Upload date:
- Size: 32.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff478fbb60b0fc7561ed4ad92b7766b775d7141d55984d470a613d15a2e7fb30
|
|
| MD5 |
a1900a613bc5fe8a857cf5b72bc56be4
|
|
| BLAKE2b-256 |
ed3867073b1a5ee113d8d7fd21958674bcd005ee1422545c27dcee0373ab407e
|