Django AppConf

1.2.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

Define your application's default settings by subclassing `AppConf` within your app (e.g., in `your_app/conf.py`). It is critical to ensure your `AppConf` subclass is imported during Django's startup. The recommended approach is to import it within your app's `AppConfig.ready()` method (defined in `your_app/apps.py`), which is guaranteed to run once Django's app registry is fully populated. Settings can then be accessed via `django.conf.settings` using the defined prefix (e.g., `MYAPP_SETTING_1`). Project-level settings in `settings.py` can override these defaults.

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.

view raw JSON →