An app configuration object to be used for handling configuration defaults of packaged apps gracefully. Say you have an app called myapp and want to define a few defaults, and refer to the defaults easily in the apps code. Add a settings.py to your app's models.py:
import appconf class MyAppConf(appconf.AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) class Meta: app_label = 'myapp'
The settings are initialized with the app label of where the setting is located at. E.g. if your models.py is in the myapp package, the prefix of the settings will be MYAPP.
The MyAppConf class will automatically look at Django's global setting to determine each of the settings. E.g. adding this to your site's settings.py will set the SETTING_1 app setting accordingly:
MYAPP_SETTING_1 = "uno"
Instead of using from django.conf import settings as you would usually do, you can optionally switch to using your apps own settings module to access the settings:
from myapp.models import MyAppConf myapp_settings = MyAppConf() print myapp_settings.MYAPP_SETTING_1
AppConf class automatically work as proxies for the other settings, which aren't related to the app. For example the following code is perfectly valid:
from myapp.models import MyAppConf settings = MyAppConf() if "myapp" in settings.INSTALLED_APPS: print "yay, myapp is installed!"
In case you want to set some settings ad-hoc, you can simply pass the value when instanciating the AppConf class:
from myapp.models import MyAppConf settings = MyAppConf(SETTING_1='something completely different') if 'different' in settings.MYAPP_SETTINGS_1: print 'yay, I'm different!'
Each of the settings can be individually configured with callbacks. For example, in case a value of a setting depends on other settings or other dependencies. The following example sets one setting to a different value depending on a global setting:
import appconf from django.conf import settings class MyCustomAppConf(appconf.AppConf): ENABLED = True def configure_enabled(self, value): return value and not self.DEBUG
The value of MYAPP_ENABLED will vary depending on the value of the global DEBUG setting.
Each of the app settings can be customized by providing a method configure_<lower_setting_name> that takes the default value as defined in the class attributes as the only parameter. The method needs to return the value to be use for the setting in question.