Providing settings with default values in Django applications.
Python
Switch branches/tags
Nothing to show
Pull request Compare This branch is 5 commits ahead of thsutton:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
application_settings
test_project
.gitignore
LICENSE
README.rst
setup.py

README.rst

Default Settings for Django Applications

version: 0.2

This repository [1] contains a proof of concept Django application that ships default values for its own settings. This is good for a number of reasons:

1. The application's settings are included in the output of manage.py diffsettings.

2. Application code can just use settings.MY_SETTING instead of getattr(settings, 'MY_SETTING', 'The default value').

3. Application settings are defined in a single place, with sane default values and, hopefully, with comments. Don't Repeat Yourself!

The actual code is rather simple: it just uses the opportunity afforded it when the application module is loaded to inject its own default settings into both the django.conf.global_settings module (Django's built-in default settings, and the source for things like the diffsettings management command) and the django.conf.settings object (being careful to check to see if it already contains a value).

To see more, take a look at:

This is slightly less than a perfect solution (being able to patch django.conf.global_settings before django.conf.settings is initialised), but the hypothetical perfection is simply not possible: the settings must be loaded before they can be used to import the INSTALLED_APPS.

Instalation

pip install django-application-settings

or latest:

git clone https://github.com/elvard/django-application-settings
cd django-application-settings
python setup.py install

Although this repository is so simple that between git repository and pip package will be delay few day at most (probably hours).

Usage

The simplest method is to use autodiscover_settings somewhere in urls.py:

autodiscover_settings()

Or you can add these lines to __init__.py inside your app if you don't want to load settings from other packages in INSTALLED_APPS:

from application_settings import provide_default_settings
provide_default_settings(__name__)

Create file settings.py inside your application directory with default settings:

# Example
PREFIX_KEY = 'value'

It's advisable to use PREFIX before all your settings names to avoid names clash.

[1]See original repository.