Public API¶
django_fastapi
¶
WebSocketAuth
¶
Resolved Django session and user for a WebSocket handshake.
aclose_db_connections()
async
¶
Release thread-sensitive leases, including during cancellation.
create_fastapi_app(config=None)
¶
Create a FastAPI app from Django settings or an explicit config.
create_websocket_app(config=None)
¶
Create a CSRF-free FastAPI app for configured WebSocket routers.
csrf_exempt(func)
¶
Mark a FastAPI path operation as exempt from Django CSRF checks.
database_sync_to_async(func, *, thread_sensitive=True)
¶
Run a synchronous ORM unit and release its connections in the same thread.
django_db(func)
¶
Bound a synchronous ORM unit, including a FastAPI handler or dependency.
Materialize querysets and response schemas inside the unit. Nested atomic blocks retain their connection until their owning outer boundary exits.
django_response_to_fastapi(response)
¶
Convert a Django response into a Starlette response.
get_authenticated_user(user)
¶
Return the current Django user or raise 401 for anonymous callers.
get_current_staff_user(user)
¶
Return the current staff user or raise 403.
get_current_user(request)
¶
Return the current Django user or AnonymousUser.
get_django_fastapi_application(fastapi_app=None, *, prefix=None, name='fastapi', websocket_app=None, websocket_path='/ws', lifespan=None, root_routes=())
¶
Create Django's ASGI app and mount a FastAPI app beside it.
get_django_request(request)
async
¶
Build and cache a Django-compatible request for a FastAPI request.
get_fastapi_prefix(config=None)
¶
Return the configured FastAPI ASGI mount prefix.
mount_django_fastapi_app(django_app, fastapi_app=None, *, prefix=None, name='fastapi', websocket_app=None, websocket_path='/ws', lifespan=None, root_routes=())
¶
Mount a FastAPI ASGI app before falling back to Django.
mount_fastapi_app(django_app, fastapi_app, *, path='/fastapi', name='fastapi')
¶
Backward-compatible alias for manually mounting a FastAPI app.
require_csrf(request, django_request)
async
¶
Require Django CSRF validation for unsafe FastAPI requests by default.
require_websocket_user(websocket)
async
¶
Return an authenticated WebSocket user or close with policy violation.
resolve_websocket_auth(websocket)
async
¶
Resolve Django's session authentication for a WebSocket handshake.
session_auth_resolver(_request, django_request)
¶
Resolve Django's normal session-authenticated user.
validate_websocket_origin(websocket)
¶
Validate Host, Origin, and the default same-origin policy.
Database lifecycle¶
django_fastapi.database.django_db(func)
¶
Bound a synchronous ORM unit, including a FastAPI handler or dependency.
Materialize querysets and response schemas inside the unit. Nested atomic blocks retain their connection until their owning outer boundary exits.
django_fastapi.database.database_sync_to_async(func, *, thread_sensitive=True)
¶
Run a synchronous ORM unit and release its connections in the same thread.
django_fastapi.database.aclose_db_connections()
async
¶
Release thread-sensitive leases, including during cancellation.
Authentication types¶
django_fastapi.auth.AuthResolver = Callable[[Request, HttpRequest], AuthResolverResult | Awaitable[AuthResolverResult]]
module-attribute
¶
django_fastapi.auth.AuthResolverResult = AuthUser | None
module-attribute
¶
django_fastapi.auth.AuthUser = AbstractBaseUser | AnonymousUser | SimpleLazyObject[Any]
module-attribute
¶
WebSocket authentication¶
django_fastapi.auth.WebSocketAuth
¶
Resolved Django session and user for a WebSocket handshake.
django_fastapi.auth.resolve_websocket_auth(websocket)
async
¶
Resolve Django's session authentication for a WebSocket handshake.
django_fastapi.auth.require_websocket_user(websocket)
async
¶
Return an authenticated WebSocket user or close with policy violation.
django_fastapi.auth.validate_websocket_origin(websocket)
¶
Validate Host, Origin, and the default same-origin policy.
ASGI composition¶
mount_django_fastapi_app and get_django_fastapi_application accept
websocket_app, websocket_path, lifespan, and root_routes keyword arguments.
root_routes is a sequence of Starlette routes placed before the exact WebSocket
route, HTTP mount, and Django fallback. Use it for project-owned root endpoints.
The root and HTTP lifespan states are merged, with root values taking precedence.
WebSocket-app lifespan hooks are not entered separately; shared resources belong
in the root lifespan. See WebSockets.