Skip to Content
DocsWhy Django-CFG

Why Django-CFG?

Django-CFG replaces a hand-written settings.py with a typed, validated Pydantic v2 model, and ships the pieces most production Django projects end up building anyway: a themed Django admin, OTP sign-in, multi-database routing, OpenAPI client generation, RQ background jobs, email, Telegram, LLM and MCP modules.

The Problem with Traditional Django

# 500+ line settings.py DEBUG = True # forgot to disable in production SECRET_KEY = "hardcoded" # secret in the repository DATABASES = {'default': {...}} # manual routing INSTALLED_APPS = [...] # dozens of apps to order by hand MIDDLEWARE = [...] # manual security setup

Typos, wrong types and missing production values are found at runtime, usually in production.

The Django-CFG Approach

# config.py from django_cfg import DjangoConfig, DatabaseConfig from .environment import env # your own pydantic-settings loader class MyConfig(DjangoConfig): project_name: str = "My App" secret_key: str = env.secret_key # min 50 chars, from the environment security_domains: list[str] = ["myapp.com"] databases: dict[str, DatabaseConfig] = { "default": DatabaseConfig( engine="django.db.backends.postgresql", name=env.database.name, ), } config = MyConfig()

config.get_all_settings() returns the complete Django settings dict. Accounts, the admin, the health endpoints and the generator are installed by default; everything else (currency, geo, payments, django_rq, mcp, notion, …) is a config section you switch on.

What You Get

Type-safe configuration

Settings are Pydantic models with extra="forbid": a misspelled field or a wrong type fails at startup with a readable error.

DatabaseConfig(engine="django.db.backends.postgresql", name="mydb", port=5432) DatabaseConfig(engine="django.db.backends.postgresql", name="mydb", prot=5432) # ValidationError

You also get IDE autocomplete for every option.

Accounts and OTP sign-in

django_cfg.apps.system.accounts is always installed: email OTP sign-in, a custom user model, profiles, activity tracking and TOTP 2FA. There is no flag to enable it.

Modern admin

The stock Django admin with the django-cfg theme (dark by default, Django’s own light/dark toggle), sidebar navigation, an Overview with dashboard tabs, and PydanticAdmin for declarative model admins.

from django_cfg import AdminSiteConfig admin_site: AdminSiteConfig = AdminSiteConfig( site_title="My Admin", )

The Overview lives at /admin/; its tabs are server-rendered pages at /cfg/admin/dashboard/<slug>/.

Multi-database routing

Give a database the apps it owns and the router does the rest:

databases: dict[str, DatabaseConfig] = { "default": DatabaseConfig(engine="django.db.backends.postgresql", name="main_db"), "analytics": DatabaseConfig( engine="django.db.backends.postgresql", name="analytics_db", apps=["analytics", "reports"], # routed automatically ), }

OpenAPI clients

from django_cfg import OpenAPIClientConfig, OpenAPIGroupConfig openapi_client: OpenAPIClientConfig = OpenAPIClientConfig( enabled=True, api_prefix="api", output_dir="openapi", drf_title="My App API", groups=[ OpenAPIGroupConfig(name="profiles", apps=["apps.profiles"], title="Profiles API"), OpenAPIGroupConfig(name="orders", apps=["apps.orders"], title="Orders API"), ], )

The generator (django_generator) produces TypeScript REST clients with Orval operations, React Query v5 hooks/query keys, Zod schemas, and MSW handlers, plus a Python client. Authentication, refresh, transport, and error handling stay in the stable @djangocfg/api runtime boundary.

Background jobs

Django-RQ is integrated through DjangoRQConfig (queues, scheduler, monitoring in the admin):

from django_rq import job @job("default") def process_document(document_id: str) -> dict: ... return {"status": "completed"} process_document.delay("123")

See Django-RQ.

Currency fields

MoneyField stores an amount with its currency and adds computed conversion and display attributes:

from django.db import models from django_cfg.modules.django_currency import MoneyField class Product(models.Model): price = MoneyField(default_currency="KRW", target_currency="USD") # adds price_currency, price_target, price_target_display, price_full_display

Communication modules

from django_cfg import DjangoEmailService, send_telegram_message DjangoEmailService().send_simple( subject="Welcome!", message="Hello from Django-CFG", recipient_list=["[email protected]"], ) send_telegram_message("System alert!", chat_id="123456")

Repeated identical Telegram messages inside dedupe_window_seconds (60 by default) are folded; pass dedupe=False per call to opt out.

LLM and MCP

from django_cfg.modules.django_llm import LLMClient client = LLMClient() response = client.chat_completion([ {"role": "user", "content": "Translate to Spanish: Hello"}, ])

The MCP module turns the project into an MCP server so AI agents can read exposed models and run allowed commands:

import os from django_cfg import MCPConfigBuilder mcp = MCPConfigBuilder() mcp.set_access_key(os.environ["MCP__ACCESS_KEY"]) mcp.expose("orders.Order", read_only=True, operations=["list", "retrieve"]) mcp.enable_public_profile() # separate personal-key surface mcp_config = mcp.build() # assign to DjangoConfig.mcp

Comparison

FeatureTraditional DjangoDjango-CFG
Type safetyruntime errorsPydantic v2 validation
Admindefault Django adminthemed Django admin, Overview
Multi-databasehand-written routerDatabaseConfig(apps=[...])
API docs and clientsmanualgenerated from the schema
User managementbasic User modelOTP, profiles, 2FA
Communicationbasic emailemail, Telegram
Background jobsmanual Celery/RQ wiringDjangoRQConfig
Loggingmanualone collapsing handler, request ids

When to Use Django-CFG

Good fit:

  • Projects that want typed, validated configuration
  • Multi-database or multi-site setups
  • Teams that want an admin, OTP sign-in and generated API clients out of the box
  • Integration-heavy services (webhooks, Telegram, email, LLM)

Not a fit:

  • Simple landing pages (overkill)
  • Pure microservices without Django or an admin (use FastAPI)
  • Projects that cannot adopt a configuration-class approach

Next Steps

  1. Installation
  2. First Project
  3. Configuration
  4. Architecture
  5. Production Deployment
  6. Migration Guide if you have an existing project
Last updated on