A library for managing default static settings for apps and projects. Define default settings for apps and override them in project settings when needed.
This library encourages loose coupling between apps by defining
the settings of each app locally. For authors of reusable apps,
it allows to avoid the
getattr pattern, and the definition of
default values scattered everywhere. Unlike other alternatives,
it avoids being too magical, and sticks to the Django conventions.
Defining settings should be simple, as they are just settings after all.
- Python 2.7, +3.4
- Django 1.8, 1.11, +2.0
pip install django-app-defaults
# my_app/defaults.py # `django.conf.settings` or any other # module can be imported and used # required for auto discoverability DEFAULT_SETTINGS_MODULE = True #: This is a doc comment for #: my setting I just created MY_DEFAULT_SETTING = "yay"
defaults's settings can be overridden in
Then anywhere within your project or app:
from app_defaults import settings print(settings.MY_DEFAULT_SETTING) # yay # All `django.conf.settings` are also available print(settings.DEBUG) # True
To load default settings for a single app instead of all of the apps, just do:
from app_defaults import Settings settings = Settings(apps=["my_app"]) # or from my_app import defaults settings = Settings(modules=[defaults])
DEFAULT_SETTINGS_MODULEvariable is not required when explicitly passing
How it works
It's an extremely simple library. It goes through all installed apps and
looks for a
defaults.py module at the root of the app. If a
var is found, then the module is loaded.
It's similar to the following pattern, but generalized into a reusable lib:
from my_app import defaults from django.conf import settings class Settings: def __getattr__(self, attr): try: return getattr(settings, attr) except AttributeError: return getattr(defaults, attr)