Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't django settings be altered at runtime?

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.

like image 980
iancoleman Avatar asked Jul 23 '12 23:07

iancoleman


2 Answers

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.

like image 73
spookylukey Avatar answered Oct 01 '22 19:10

spookylukey


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.

like image 44
Vinod Kurup Avatar answered Oct 01 '22 19:10

Vinod Kurup