Skip to main content

UNKNOWN

Project description

django-modelsettings

This Django application allows to define Django application settings with Django ORM models and edit them in the admin area.

Please check the other alternatives at https://www.djangopackages.com/grids/g/live-setting/. This project started because I tried them all and I was not really happy with any of them.

Key points

  1. The settings are stored in the database, in normalized tables with proper field types and constraints. In particular, it is possible to have a setting which is a ForeignKey to another model (e.g. “Default account”), and it will properly validate and/or update when the corresponding model is deleted.

  2. The settings are described using standard Django ORM classes (like EmailField or SmallPositiveIntegerField) with all the bells and whistles (e.g. validators). There is no parallel hierarchy of classes for different types of data.

  3. Each application may have any number of settings groups, however there is no fancy and complex syntax for that. The developer may simply add more Settings classes if he decides so.

  4. The settings are lazy and effective. The database is not hit until the settings are actually accessed, and then it takes exactly one SQL query to fetch all settings for all apps.

  5. The system behaves correctly when there is no corresponding data in the database (if the settings have never been saved yet, or have been saved partly).

  6. Programmatically, all settings from all application are accessible via a single object.

  7. There is a page for Django Admin.

Requirements

The latest version supports Django 1.7-1.9.

django-modelsettings 0.1.x had a different, more hackish API and supported Django 1.4-1.8.

Installation

pip install django-modelsettings

Usage

Add dbsettings to INSTALLED_APPS:

# settings.py

INSTALLED_APPS = [
        ...
        'dbsettings',
]

Add Settings class in your application:

# blog/models.py

from dbsettings.models import BaseSettings

class Settings(BaseSettings):
        contact_email = models.EmailField(default="info@localhost")
        update_interval = models.PositiveIntegerField(null=True, default=10, help_text="Update interval in seconds")
        facebook_app_id = models.CharField("Facebook App ID", max_length=32, blank=True)

Create the corresponding database tables:

./manage.py makemigrations && ./manage.py migrate

In your business logic code, access settings directly:

from dbsettings import settings

print(settings.blog.contact_email)

settings.blog.update_interval = 60
settings.blog.save()

…or via django.conf.settings.db shortcut:

from django.conf import settings

print(settings.db.blog.contact_email)

settings.db.blog.update_interval = 60
settings.db.blog.save()

Admin area

Enable the admin area page by adding a route:

# urls.py

import dbsettings.urls

urlpatterns = [
        url(r'^admin/settings/', include(dbsettings.urls)),
        ...
]

The settings editor will now be available under Django Admin > Settings > Settings.

You can also add a direct link with <a href="{% url 'dbsettings' %}">{% trans "Settings" %}</a> (e.g. in your admin/base_site.html overrides).

Several groups of settings per application

It is possible to split settings into several groups within one application.

# blog/models.py

from dbsettings.models import BaseSettings

class Settings(BaseSettings):
        option1 = models.IntegerField()

class Foo(BaseSettings):
        option2 = models.IntegerField()

class Bar(BaseSettings):
        option3 = models.IntegerField()
from dbsettings import settings

print(settings.blog.option1)
print(settings.blog_foo.option2)
print(settings.blog_bar.option3)

Project details


Download files

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

Source Distribution

django-modelsettings-0.2.3.tar.gz (5.3 kB view details)

Uploaded Source

File details

Details for the file django-modelsettings-0.2.3.tar.gz.

File metadata

File hashes

Hashes for django-modelsettings-0.2.3.tar.gz
Algorithm Hash digest
SHA256 2ecd6c0e9457ac4f86e827eea5b5ead06493b06600335fbd8af8806d8ceccfb4
MD5 900d0cbe3e8d8179a30122e149a63484
BLAKE2b-256 c466c9351b6fd963a0c1c6c69e2456a0fc59c68f15e307c87facc86e848e4f95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page