Public API and Lazy Imports
django_cfg has no separate registry module. Its public API is the explicit facade in
django_cfg/__init__.py: a list of imports grouped by area, an __all__, and a small
module-level __getattr__ for the few names that cannot be imported eagerly.
How It Works
Most public names are plain imports at the top of django_cfg/__init__.py:
from django_cfg import DjangoConfig, DatabaseConfig, CacheConfig
from django_cfg import DjangoEmailService, send_email, send_telegram_message
from django_cfg import ConfigurationError, ValidationErrorNames that read Django settings at import time are resolved lazily on first access:
# django_cfg/__init__.py (shape)
_LAZY_IMPORTS: dict[str, tuple[str, str]] = {
"DefaultPagination": ("django_cfg.middleware.pagination", "DefaultPagination"),
"ImportExportModelAdmin": ("django_cfg.modules.django_import_export", "ImportExportModelAdmin"),
"check_python_version": ("django_cfg.utils.version_check", "check_python_version"),
# ...
}
def __getattr__(name: str):
if name in _LAZY_IMPORTS:
module_path, class_name = _LAZY_IMPORTS[name]
import importlib
return getattr(importlib.import_module(module_path), class_name)
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")The lazy set is: the DRF pagination classes (DefaultPagination, LargePagination,
SmallPagination, NoPagination), the django_import_export classes (ImportForm,
ExportForm, SelectableFieldsExportForm, ImportExportMixin, ImportExportModelAdmin,
ExportMixin, ImportMixin, BaseResource) and check_python_version. They import Django
models or read REST_FRAMEWORK, which fails before django.setup().
Public Names by Area
| Area | Names |
|---|---|
| Core | DjangoConfig, StartupInfoMode, set_current_config, add_django_cfg_urls, get_django_cfg_urls_info, admin_urlpatterns |
| Exceptions | DjangoCfgException, ConfigurationError, ValidationError, DatabaseError, CacheError, EnvironmentError |
| Infrastructure | DatabaseConfig, CacheConfig, SecurityConfig, LoggingConfig |
| API | DRFConfig, SpectacularConfig, JWTConfig, OAuthConfig, GitHubOAuthConfig, GoogleOAuthConfig, LimitsConfig, ApiKeys, TwoFactorConfig, AccountEmailsConfig |
| Django models | EnvironmentConfig, AxesConfig, DjangoRQConfig, RQQueueConfig, RQScheduleConfig, CurrencyConfig, PaymentsConfig, GeoConfig, StorageConfig, S3StorageConfig, ConstanceConfig, OpenAPIClientConfig, Site, SiteConfig and others |
| Services | EmailConfig, TelegramConfig, NotionConfig, DjangoEmailService, send_email, DjangoTelegram, send_telegram_message, send_telegram_photo |
| Admin | AdminConfig, FieldConfig, FieldsetConfig, ActionConfig, WidgetRegistry, Icons, IconCategories |
| Admin site | AdminSiteConfig, AdminSidebar, NavigationItem, NavigationSection, NavigationItemType, SiteDropdownItem |
| MCP | DjangoMCPModuleConfig, MCPConfigBuilder, MCPProfile and the MCP_* helpers |
| Testing | SmartTestRunner, FastTestRunner |
The complete list is django_cfg.__all__:
import django_cfg
print(django_cfg.__all__)Guarding the API
tests/test_public_api.pycomparesdjango_cfg.__all__withtests/public_api_snapshot.json, so a removed name fails the test.tests/test_import_budget.pymeasuresimport django_cfgin a clean subprocess: a ceiling on loadeddjango_cfgmodules, and heavy packages (openai,boto3,pandas, …) that must stay unloaded.
See System Architecture for the layering contracts.
Best Practices
Import what you need
from django_cfg import DjangoConfig, DatabaseConfig # good
from django_cfg import * # avoid: pulls the whole facadeOptional dependencies
MCP names are None when the MCP optional dependency is missing:
from django_cfg import DjangoMCPModuleConfig
if DjangoMCPModuleConfig is None:
... # the MCP dependency is not installedImport modules by their own path when a name is not in the facade
Not every class is re-exported. Import it from its module:
from django_cfg.modules.django_admin.base import PydanticAdmin
from django_cfg.modules.django_currency.widgets import MoneyFieldWidgetRelated Documentation
- Configuration Guide - Configure Django-CFG
- Modules System - Feature modules
- Utilities - Environment detection and defaults