Changing Django settings at runtime

AFAIK, the Django settings are supposed to be immutable. There are multiple reasons for this, the most obvious being that Django is not aware of the server’s execution model (prefork / multi-threaded).

Also, you can’t load the settings themselves from a Django model because the settings need to be loaded before you can use anything in the ORM.

So, basically, you have two solutions:

  • you can bootstrap the settings from the database by using any lower-level database access mechanism to load them; or
  • you can just define your settings in some other model and fetch them directly when you need them.

The first is an incredible hack and I don’t suggest it. The second is much more direct and cleaner, but requires you to change your usual habits (from django.conf import settings).

The second approach is probably what’s implemented by the 3rd-party apps you linked to.

Leave a Comment