9

The django docs clearly states

You shouldn't alter settings in your applications at runtime.

Here's the link to that statement

My question is, why is this so? I want to add applications dynamically at runtime, and add databases at runtime, both of which involve editing the settings. Can someone explain why settings are not to be edited at runtime and if exceptions exist, which settings they are and why they are exceptional? I'm not so much interested in how to achieve my goal, but in the reason for why settings shouldn't be altered.

iancoleman
  • 2,766
  • 2
  • 19
  • 17
  • I had a look at https://docs.djangoproject.com/en/dev/ref/settings/, and it seems to me these are all settings that apply on *application startup.* Consequently, it wouldn't make much sense to modify them at runtime. – Robert Harvey Jul 23 '12 at 23:19

3 Answers3

8

Most settings will not be re-read if you change them at runtime. So Django will not recognise the changes you make.

This is due to the fact that Django is just normal Python code. It isn't like a server that is monitoring your code - it is just part of your code.

In some cases, parts of Django code might respond to changes in settings, because they might do 'settings.DEFAULT_FROM_EMAIL' every time mail is sent, for example.

But if Django processes the setting in any way, like it has to do for INSTALLED_APPS, it isn't going to notice you changed something and re-do the processing.

Which settings are safe? Well, the docs are saying "none are safe", because it might change in the future. Django might save a copy of any setting for some reason, or do some processing.

Changing INSTALLED_APPS could never be made to work, because it alters which modules are imported. There is simply no way that Django could work around the way that Python works at this level - it would need to be able to 'unimport' modules, which is basically impossible (the only way is to restart the process), and there are other problems associated with cross-app links.

spookylukey
  • 6,380
  • 1
  • 31
  • 34
1

AFAIK there is no documentation on which settings are safely modifiable at runtime, but there is an open ticket asking that they be documented more clearly.

Vinod Kurup
  • 2,676
  • 1
  • 23
  • 18
  • How about one's own settings variable? e.g. create an empty dictionary in settings, and add data to it over the period that the system is running (in this case its irrelevant if the data is lost on a system restart). – Derek Mar 27 '17 at 06:45
1

If you take a look under the hood at the settings object Django exposes to interface with your project's settings module, you'll notice that there's nothing preventing you to dynamically change the settings at runtime.

You should, however, appreciate that the framework's architecture is built around a request-response flow where a lot of global state is being shared between threads for memory optimization, based on the premise that the application is configured only once during initialization.

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114