Skip to content

Repository files navigation

django-fastapi

PyPI Python CI Documentation License: MIT

Run FastAPI beside an existing Django application while keeping Django in charge of settings, models, migrations, sessions, and users.

django-fastapi is an ASGI bridge. It does not add FastAPI routes to Django's urls.py, replace Django middleware, or generate API schemas from Django models. It creates a FastAPI application and mounts it before Django's ASGI fallback.

ASGI server
└── combined application
    ├── /api/* → FastAPI
    └── /*      → Django ASGI

Installation

python -m pip install django-fastapi
python -m pip install uvicorn  # or daphne

Five-minute setup

Create a router in any Django app:

# books/api.py
from fastapi import APIRouter

router = APIRouter(prefix="/books", tags=["books"])


@router.get("/health")
def health() -> dict[str, str]:
    return {"status": "ok"}

Configure the bridge in Django settings:

DJANGO_FASTAPI = {
    "PREFIX": "/api",
    "TITLE": "Example API",
    "ROUTERS": ["books.api.router"],
}

Mount it in the project's ASGI entrypoint:

# project/asgi.py
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

from django_fastapi import get_django_fastapi_application

application = get_django_fastapi_application()

Run the combined application:

uvicorn project.asgi:application

FastAPI is now available at /api, including OpenAPI at /api/docs. Existing Django URLs continue to use the Django ASGI application.

Django user and session access

The default resolver uses Django's normal session cookie:

from typing import Annotated, Any

from fastapi import Depends
from django_fastapi import get_authenticated_user


@router.get("/me")
def me(
    user: Annotated[Any, Depends(get_authenticated_user)],
) -> dict[str, str]:
    return {"username": user.get_username()}

CSRF protection is enabled by default for unsafe methods. Custom JWT, OAuth, or API-key authentication can be added with an auth resolver without changing the bridge.

Existing WSGI deployments

FastAPI requires an ASGI server, but an existing synchronous Django deployment does not need to be migrated all at once. Run one WSGI service for existing Django URLs and a second ASGI service for the FastAPI prefix, then route both through the same reverse proxy:

Nginx
├── /api/* → ASGI service → FastAPI + Django ASGI
└── /*      → WSGI service → Django WSGI

Both services must run the same code and share Django settings, database, session store, cookie settings, and secrets. See the deployment guide for complete Nginx, Docker Compose, and Kubernetes examples.

Documentation

Development

python -m pip install -e ".[test,lint,docs,release]"
pytest
ruff check .
ruff format --check .
mypy
mkdocs build --strict

See CONTRIBUTING.md before opening a pull request.

License

MIT

About

Run FastAPI beside Django with shared settings, ORM, sessions, authentication, and CSRF protection.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages