Utilities - Smart Defaults & Version Management
Django-CFG provides a comprehensive utility system with smart defaults and version management to enhance developer experience and ensure compatibility.
Overview
The utilities cover:
- Smart defaults: environment-aware Django settings fragments used when the config does not set a value
- Version check: Python version guard with upgrade hints
- Environment detection:
DJANGO_ENV/ENVIRONMENT/ENVresolution
Smart Defaults
django_cfg.utils.smart_defaults.SmartDefaults is a set of static methods that return plain dicts of Django settings. The settings generators call them; you rarely call them yourself.
| Method | Returns |
|---|---|
get_database_defaults(environment, debug, engine) | DATABASES entry: CONN_MAX_AGE=0, CONN_HEALTH_CHECKS, psycopg pool options for PostgreSQL, timeout for SQLite |
get_cache_defaults() | LocMem CACHES["default"] |
get_middleware_defaults() / get_installed_apps_defaults() | baseline middleware and apps |
get_templates_defaults() / get_static_files_defaults() | TEMPLATES, static and media settings |
get_rest_framework_defaults() / get_cors_defaults() | DRF and CORS baselines |
get_logging_defaults(...) | logging dict (see Logging) |
from django_cfg.utils.smart_defaults import SmartDefaults, get_pool_config
db = SmartDefaults.get_database_defaults(
environment="production",
engine="django.db.backends.postgresql",
)
pool = get_pool_config("production") # {'min_size': 10, 'max_size': 50, ...}Pool size is read from DB_POOL_MIN_SIZE, DB_POOL_MAX_SIZE and DB_POOL_TIMEOUT. Set AUTO_POOL_SIZE=true to size the pool from ASGI/WSGI detection.
SmartDefaults.configure_cache_backend() and configure_email_backend() are thin: they return the config you pass, or a default CacheConfig() / EmailConfig() when you pass None. Security settings are built by SecurityBuilder, not by SmartDefaults.
Cache and email backends
Backend selection happens in the config models, not through a backend="auto" argument:
from django_cfg import CacheConfig
# No redis_url: LocMem cache. With redis_url: Redis.
cache = CacheConfig(redis_url="redis://localhost:6379/1", timeout=300, key_prefix="myapp")CacheConfig fields: redis_url, timeout, max_connections, key_prefix, version, connection_pool_kwargs, compress, serializer, backend_override. In DjangoConfig you usually set redis_url once and the default cache is created from it. EmailConfig.backend is one of smtp, console, file, memory, gateway; the default is console.
Environment Detection
django_cfg.core.environment.EnvironmentDetector.detect_environment() reads, in order, DJANGO_ENV, ENVIRONMENT, ENV, then falls back to the DEBUG flag and finally development.
On DjangoConfig, env_mode is one of development, production, test (default production); while it is still the default, DJANGO_ENV, ENVIRONMENT or ENV switch it. Use config.is_development, config.is_production, config.is_test.
Python Version Check
from django_cfg.utils.version_check import (
check_python_version,
get_python_version_string,
is_python_compatible,
)
check_python_version("MyProject") # prints upgrade hints and exits if Python < 3.12
print(get_python_version_string()) # "3.12.4"
print(is_python_compatible()) # True on 3.12+django-cfg supports Python >=3.12,<3.14.
Using it in settings.py
# settings.py
from django_cfg.utils.version_check import check_python_version
from .config import config
check_python_version("MyProject")
globals().update(config.get_all_settings())Related Documentation
- Configuration Guide - Core configuration
- Public API - Facade and lazy imports
- Modules System - Available modules
- CLI Tools - Utility commands
The Utilities system provides the foundation for Django-CFG’s intelligent, environment-aware configuration management.