Skip to main content

A simple, extensible YAML-based configuration strategy for Django

Project description

Settings Manager

This project provides a simple and extensible strategy for managing Django settings in YAML files that can be loaded from multiple locations.

Settings files

The settings manager takes a list of directories and loads all *.yaml or *.yml files in them. To control the order of loading, set _meta.priority. For example:

  priority: 10

Generally, it will be useful to set all files providing variables to a low priority, set base configuration file to a higher priority, and local configuration overrides to the highest priority. For example:

# variables.yml
_meta: {priority: 0}
  db_name: my_database

# base.yml
_meta: {priority: 10}
  DEBUG: false

# local.yml
_meta: {priority: 20}
  DEBUG: true
  DATABASE_NAME: '{db_name}'

Settings and Variables

Settings are Django settings that get applied to the module and would traditionally be set in Variables are useful for parameterizing configurations, or for getting external values using a handler.

Settings and variables in YAML can be specified directly, such as:

  db_user: postgres

  DEBUG: false

Or they can be provided by a registered handler. The following example gets the value of the environment variable DJANGO_DB_PASSWORD and assigns it to the configuration variable db_password.

      - name: get_env
        kwargs: {key: DJANGO_DB_PASSWORD}

Interpolating variables

Now, the password set above can be used with:

      PASSWORD: '{db_password}'


Handlers process data and return a value that gets assigned, typically to a setting. Custom handlers can be registered by extending the AbstractConfigHandler class and registering an instance of the class like this:

class GetRandomHandler(AbstractConfigHandler):
    def get_value(self, context, **kwargs):
        return rand(kwargs['min'], kwargs['max'])

class AsStringHandler(AbstractConfigHandler):
    def get_value(self, context, **kwargs):
        return str(kwargs['value'])

loader = ConfigLoader()
    'get_random': GetRandomHandler(),
    'as_string': AsStringHandler(),

In Yaml, the following could use the custom handlers to generate a random number and provide it as a string value.

      - name: get_random
        kwargs: {min: 0, max: 5}
      - name: as_string

Implementation in a Django project

The following code loads all yaml files from the ./conf directory, plus any files specified by the DJANGO_CONFIG_PATHS environment variable.

For the example, the assumed file structure is:

# File:
import sys
import os
from settings_manager.loader import ConfigLoader

# Set the module's base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

# Set the default configuration directory
DEFAULT_CONF_DIR = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), "conf")

# Create the loader
loader = ConfigLoader(sys.modules[__name__])

# Load all yaml files from comma-separated list of directories provided by environment variable 'DJANGO_CONFIG_PATHS'.
paths = [p.strip() for p in os.environ.get("DJANGO_CONFIG_PATHS", "").split(',') if p != '']

# Insert the base settings directory into the config paths.
paths.insert(0, DEFAULT_CONF_DIR)

# Load the configuration.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-settings-manager, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size django_settings_manager-0.0.1-py3-none-any.whl (5.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size django-settings-manager-0.0.1.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page