API

djconfig module

djconfig.config djconfig.conf.Config object (singleton)

Contain registry of config forms and cache of key-value matching the forms field-value.

All methods are private to avoid clashing with the dynamic attributes.

This should be usually accessed through config

djconfig.register djconfig.conf.Config._register attribute

Register a config form into the registry

Parameters:
djconfig.reload_maybe djconfig.conf.Config._reload_maybe attribute

Reload the config if the config model has been updated. This is called once on every request by the middleware. Should not be called directly.

Config Object

class djconfig.conf.Config[source]

Contain registry of config forms and cache of key-value matching the forms field-value.

All methods are private to avoid clashing with the dynamic attributes.

This should be usually accessed through config

ConfigForm Object

class djconfig.forms.ConfigForm(*args, **kwargs)[source]

Base class for every registered config form. It behaves like a regular form.

Inherits from django.forms.Form. The initial attr will be updated with the config values if any.

All form fields implementing this, should have a unique name to avoid clashing with other registered forms, prefixing them with the app name is a good practice.

Parameters:
  • args – Positional parameters passed to parent class
  • kwargs – Keyword parameters passed to parent class
save()[source]

Save the config with the cleaned data, update the last modified date so the config is reloaded on other process/nodes. Reload the config so it can be called right away.

Template Context Processors

djconfig.context_processors.config(request)[source]

Simple context processor that puts the config into every RequestContext. Just make sure you have a setting like this:

TEMPLATE_CONTEXT_PROCESSORS = (
    # ...
    'djconfig.context_processors.config',
)

Middlewares

class djconfig.middleware.DjConfigMiddleware(get_response=None)[source]

Populate the cache using the database. Reload the cache only if it is not up to date with the config model

Test Helpers

djconfig.utils.override_djconfig(**new_cache_values)[source]

Temporarily override config values.

This is similar to django.test.override_settings(), use it in testing.

Parameters:new_cache_values – Keyword arguments, the key should match one in the config, a new one is created otherwise, the value is overridden within the decorated function

Admin

This module allows to register a config into django’s admin.

Usage:

class FooConfigAdmin(djconfig.admin.ConfigAdmin):
    change_list_form = FooConfigForm


class FooConfig(djconfig.admin.Config):
    app_label = 'djconfig'
    verbose_name_plural = 'foo config'
    name = 'fooconfig'

djconfig.admin.register(FooConfig, FooConfigAdmin)
djconfig.admin.register(conf, conf_admin, **options)[source]

Register a new admin section.

Parameters:
  • conf – A subclass of djconfig.admin.Config
  • conf_admin – A subclass of djconfig.admin.ConfigAdmin
  • options – Extra options passed to django.contrib.admin.site.register
class djconfig.admin.ConfigAdmin(model, admin_site)[source]

A ConfigAdmin is subclass of django.contrib.admin.ModelAdmin.

change_list_form class var must be set to a valid djconfig.forms.ConfigForm subclass

class djconfig.admin.Config[source]

A Config is akin to django’s model Meta class.

app_label must be a valid installed app, 'djconfig' may be used for every registered form, if they don’t belong in a particular app. verbose_name_plural is the title of the admin’s section link, it can be anything. The (app_label, verbose_name_plural, name) must be unique together across registered forms. name is used as the link slug, and it might be used in other places, valid chars are [a-zA-Z_]