Django AppConf
django-appconf is a helper class designed to manage configuration defaults gracefully within packaged Django applications. It simplifies defining, documenting, and overriding application-specific settings, preventing direct manipulation of `django.conf.settings`. As of version 1.2.0, it remains actively maintained, with a focus on modern Python and Django versions.
Warnings
- gotcha AppConf subclasses must be imported during Django's startup phase for settings to be correctly applied. The official documentation recommends placing `AppConf` subclasses in `models.py` or importing them via the `ready()` method of your application's `AppConfig` class.
- gotcha Do not confuse `django-appconf` with Django's built-in `AppConfig` class (from `django.apps`). They serve different purposes: `django-appconf` manages application-specific settings and defaults, while `AppConfig` stores metadata for an application within Django's app loading mechanism. They are not interchangeable.
- gotcha When defining a `configure_<setting_name>` method within your `AppConf` subclass for custom logic, always ensure the method explicitly returns a value. Forgetting to return a value will result in the setting effectively becoming `None`.
- deprecated The `app_label` attribute within the inner `Meta` class of `AppConf` was renamed to `prefix` in version 0.4. While older versions might have offered backward compatibility, for current versions (>=1.0), `prefix` should be used to explicitly define the setting prefix.
Install
-
pip install django-appconf
Imports
- AppConf
from appconf import AppConf
Quickstart
import os
from django.apps import AppConfig
from appconf import AppConf
# app_name/conf.py
class MyAppConf(AppConf):
MYAPP_SETTING_1 = "default_value_1"
MYAPP_SETTING_2 = {
"key": "default_value_2"
}
class Meta:
prefix = 'MYAPP' # Explicitly set prefix, often defaults to capitalized app label
def configure_myapp_setting_1(self, value):
# Example of custom configuration logic, must return a value
if os.environ.get('OVERRIDE_SETTING_1'):
return os.environ.get('OVERRIDE_SETTING_1')
return value
# app_name/apps.py
class MyAppConfig(AppConfig):
name = 'app_name'
verbose_name = 'My Application'
def ready(self):
super().ready()
# Import AppConf subclass here to ensure it's loaded during startup
try:
# This import triggers the MyAppConf to load its settings
from . import conf
except ImportError:
pass # Handle gracefully if conf.py doesn't exist or has issues
# Example of how settings are accessed elsewhere (e.g., views.py, models.py)
# Ensure Django settings are configured (e.g., through a Django project's settings.py)
# In a Django project's settings.py, you would add:
# INSTALLED_APPS = [
# 'app_name.apps.MyAppConfig',
# # ... other apps
# ]
# MYAPP_SETTING_1 = "project_override_value"
# To demonstrate accessing the settings after Django is ready
from django.conf import settings
def get_my_settings():
# Accessing settings via django.conf.settings is recommended
setting1 = settings.MYAPP_SETTING_1
setting2 = settings.MYAPP_SETTING_2
return f"Setting 1: {setting1}, Setting 2: {setting2}"
# This part would typically be run within a Django context
# For demonstration, simulate Django setup minimaly if not in a full project
if not hasattr(settings, 'configure'):
from django.conf import settings
settings.configure(
INSTALLED_APPS=['app_name.apps.MyAppConfig'],
MYAPP_SETTING_1="configured_default_via_settings",
SECRET_KEY='a-very-secret-key',
DEBUG=True
)
# Initialize Django apps - necessary for AppConfig.ready() to run
import django
django.setup()
print(get_my_settings())
# Expected output will depend on whether MYAPP_SETTING_1 is overridden in global settings or by the environment variable.