DEV Community

Krishna Modepalli
Krishna Modepalli

Posted on

Stop Putting Everything in .env β€” Runtime Config for Django

Hello all! πŸ‘‹

TL;DR I created a runtime configuration Django app called django-sysconfig.

Django Sysconfig app usage

Here's the problem that led me to build it. We all tend to bloat the settings.py and .env files to store all these secrets and other API URLs for 3rd-party integrations. This approach requires the server restart on each change to .env file, and also this is not a scalable solution.

The Solution

django-sysconfig is a schema-driven database-backed typed runtime configuration app for Django. This app has in-built cache support (using Django Cache Framework) with a simple Admin UI to allow non-IT admins to easily manage the project configurations.

Some features of this app:

  1. Validations (email, url, ip, port, hostname, min-max, regex, etc. Each config value will be validated before saving to DB)
  2. A clean Admin UI to manage configurations.
  3. Schema based config. Your schema updates as your app grows and is distributed across your apps instead of a single place)
  4. on_save callbacks for each config (helpful for DevOps notifications)
  5. CLI management command to manage configs through cli.
  6. Runtime typed. The values are automatically converted to the field defined type.
  7. Secrets. Store secrets securely in DB (encrypted) and are decrypted when used in code.

This app is built to be extended from the core. You don't like the admin UI. Make your own the core config API and simple templates. Need new validators or new config Field types? Create your own very easily.

How does it compare?

Feature django-sysconfig ⭐ django-constance django-dynamic-preferences
Schema definition sysconfig.py per app, auto-discovered CONSTANCE_CONFIG dict in settings.py Decorated Python class, registered manually
Storage Database Redis, Database, or Memory Database
Caching βœ… No expiry, invalidated on write. βœ… Redis TTL or DB cache backend βœ… Django cache, invalidated on write
Typed fields βœ… String, Int, Decimal, Bool, Select, Textarea, Secret Basic β€” custom types via CONSTANCE_ADDITIONAL_FIELDS βœ… Bool, String, Int, Float, Choice, File, ModelChoice…
Built-in validators βœ… 20 β€” email, URL, IP, range, regex, slug, JSON, port… ❌ Basic form field validation only
Encryption at rest βœ… SecretFrontendModel (Fernet AES-128 + HMAC) ❌ ❌
on_save callback βœ… Per-field, fires after DB write + cache update ❌ ❌
Management CLI βœ… get, set, reset, export, import (with --dry-run, --force, --skip-on-save-callbacks) ❌ ❌
Per-user / instance settings ❌ Global only ❌ Global only βœ… Global + per-user/model
Admin UI Custom staff-only views at /admin/config/ Standard Django admin (flat list) Standard Django admin (grouped by section)
Accessing values config.get("app.section.field") config.SETTING_NAME global_preferences["section__name"]
REST API ❌ ❌ βœ… Optional, DRF-based
Min Python / Django Python 3.11 / Django 4.2 Python 3.x / Django 3.2 Python 3.6 / Django 3.2
Maturity πŸ†• Early stage πŸ† Mature, Jazzband-maintained βœ… Stable, active community

I've recently released the v1.0.0 to PyPi. Feel free to try it out. Would love your feedback on this.

GitHub Repo: https://github.com/krishnamodepalli/django-sysconfig
Docs: https://krishnamodepalli.github.io/django-sysconfig/

Top comments (0)