Production-ready FastAPI template with Django ORM, admin panel, and Celery background tasks β featuring dependency injection, type-safe configuration, and comprehensive observability.
- HTTP API β FastAPI with automatic OpenAPI documentation
- Background Tasks β Celery with beat scheduler
- Dependency Injection β punq IoC container
- Type-Safe Config β Pydantic Settings with validation
- Observability β Logfire (OpenTelemetry) integration
- Production Ready β Docker Compose with PostgreSQL, PgBouncer, Redis, MinIO
Define a service with business logic and database operations:
# src/core/todo/services.py
from django.db import transaction
from core.todo.models import Todo
class TodoService:
def get_todo_by_id(self, todo_id: int) -> Todo | None:
return Todo.objects.filter(id=todo_id).first()
def list_todos(self, user_id: int) -> list[Todo]:
return list(Todo.objects.filter(user_id=user_id))
@transaction.atomic
def create_todo(self, user_id: int, title: str) -> Todo:
return Todo.objects.create(user_id=user_id, title=title)Create a controller β services are auto-injected via the IoC container:
# src/delivery/http/todo/controllers.py
from dataclasses import dataclass
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from core.todo.services import TodoService
from delivery.http.auth.jwt import AuthenticatedRequest, JWTAuth, JWTAuthFactory
from infrastructure.delivery.controllers import Controller
class TodoSchema(BaseModel):
id: int
title: str
completed: bool
@dataclass
class TodoController(Controller):
_jwt_auth_factory: JWTAuthFactory
_todo_service: TodoService # Auto-injected
def register(self, registry: APIRouter) -> None:
registry.add_api_route(
path="/v1/todos",
endpoint=self.list_todos,
methods=["GET"],
dependencies=[Depends(self._jwt_auth_factory())],
)
def list_todos(self, request: AuthenticatedRequest) -> list[TodoSchema]:
todos = self._todo_service.list_todos(user_id=request.state.user.id)
return [TodoSchema.model_validate(t, from_attributes=True) for t in todos]The Golden Rule: Controllers never access models directly β all database operations go through services.
Before getting started, ensure you have installed:
- uv β Blazingly fast Python package manager (Install uv)
- Docker & Docker Compose β For infrastructure services (Install Docker)
git clone https://github.com/MaksimZayats/fastdjango.git
cd fastdjangoReplace the default project name in pyproject.toml with your own.
Use lowercase letters, numbers, and hyphens (e.g., my-awesome-api, backend-service).
uv sync --locked --all-extras --devcp .env.example .envThe .env.example contains sensible defaults for local development. Key variables:
DJANGO_SECRET_KEYβ Django secret keyJWT_SECRET_KEYβ JWT signing keyDATABASE_URLβ PostgreSQL connection stringREDIS_URLβ Redis connection string
docker compose up -d postgres redis minioThis starts:
- PostgreSQL 18 β Primary database
- PgBouncer β Connection pooling (transaction mode)
- Redis β Cache and Celery broker
- MinIO β S3-compatible object storage
docker compose up minio-create-buckets migrations collectstaticThis runs one-time setup tasks:
- Creates MinIO buckets for static/media files
- Applies Django database migrations
- Collects static files to MinIO
make devThe API is available at http://localhost:8000 with interactive docs at /api/docs.
curl http://localhost:8000/api/v1/healthExpected response:
{
"status": "ok"
}Full documentation is available at template.zayats.dev.
| Section | Description |
|---|---|
| Quick Start | Get running in 5 minutes |
| Project Structure | Understand the codebase organization |
| Development Environment | IDE setup and tooling |
| Tutorial: Build a Todo List | Learn by building a complete feature |
| Concepts | Service layer, IoC, controllers, factories |
| How-To Guides | Add domains, tasks, secure endpoints |
| Reference | Environment variables, Makefile, Docker |
| Component | Technology | Documentation |
|---|---|---|
| HTTP API | FastAPI 0.128+ | fastapi.tiangolo.com |
| ORM & Admin | Django 6+ | docs.djangoproject.com |
| Task Queue | Celery 5.x | docs.celeryq.dev |
| Validation | Pydantic 2.x | docs.pydantic.dev |
| Settings | Pydantic Settings | docs.pydantic.dev/settings |
| IoC Container | punq | github.com/bobthemighty/punq |
| Observability | Logfire | Logfire docs |
| Package Manager | uv | docs.astral.sh/uv |