Changing Django settings at runtime

Viewed 59099

I'd like to expose some (app-specific) settings to the admin interface, so users can change them comfortably and also not have to restart Django.

How should I go about this?

I checked out the applications on http://djangopackages.com/grids/g/live-setting/ (btw django-constance was the most appealing) but really what all these apps are doing is storing values in a database, providing a web interface to change them, and caching. Aren't the first two features already built into Django?

The biggest drawbacks I see are that none of the apps are drop-in replacements for the old location of these settings (settings.py), and require me to migrate to their notation, and often add another context processor to access them in templates.

Couldn't I just do this?

  1. Create a model for my settings (this gives me the various types and validation)
  2. Instantiate one such object to hold my settings (this allows the users to edit them in the admin interface) - I could dump defaults as fixtures like for other models
  3. Wrap settings.py so it makes a database query for my settings - http://www.loose-bits.com/2011/04/extending-django-settings-with-derived.html

From my current, naive point of view the only drawbacks I see would be:

  1. Adding or changing the available settings requires a schema migration (south). - I can live with that.
  2. I have a model with possibly multiple instances but really only need a singleton. - That could actually be a useful feature at some point.
  3. Performance/Caching: Looking at http://code.djangoproject.com/svn/django/trunk/django/conf/ I'd have to put a little bit of cleverness into the settings wrapper and/or model, so that model changes clear or update cached values. - doesn't seem to be rocket science.
  4. Doing the same in another project would require a similar effort again. - I think a single dictionary constant in settings.py, holding model name(s) and field names for the lookups is all that would differ.

Wouldn't this be the best of both worlds - runtime admin (with all its perks), database backend, caching, and none of my settings.USED_TO_BE_IN_SETTINGS_DOT_PY would need any changing. Am I missing something?

8 Answers

You can use recomended .configure() method of settings module:

from django.conf import settings
settings.configure(DEBUG=True)

settings module has additional handy features. Check docs.

You cannot directly modify the settings.py file For example: If u want change the database at runtime, you should Separate the configuration of the database

# Projecr_name/user_database.py
user_database = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'name',
    'USER': 'admin',
    'PASSWORD': '111111',
    'HOST': '127.0.0.1',
    'PORT': '3306'
},
'user_db': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'user1',
    'USER': 'admin',
    'PASSWORD': '22222',
    'HOST': '127.0.0.1',
    'PORT': '3306'
}
}
# Projecr_name/settings.py
from .user_database import user_database
...
DATABASES = user_database
...

Call in your logical view

# view.py
from ../Projecr_name/user_database import user_database
class Some(...):
    def Some(request):
    user_database['user_db']['NAME']='user2'

then you can change any setting at runtime use this way

If you have a variable in the settings.py and you want it to change at any time there is 2 ways the First one is to make a table in database then make for it a serializer then make a view set and any time you want to get it you could send a http request to this viewset then retrieve the data Second one is to use caching it is so fast and it is familiar to localStorage in angular

Related