2

I have read that settings variable should not be altered at runtime also mentioned here. However if we define a custom settings variable, can it be altered at runtime? Are there any drawbacks if I do this. Ex: settings.CUSTOM_VAR = '20' #done in one of the views

Community
  • 1
  • 1
Dalon
  • 53
  • 6
  • The problem is Django can not recognize those modifications in some cases. For example, imagine you change INSTALLED_APPS in runtime, that settings defines which modules can be imported and which modules cannot... would be a total mess. – lapinkoira Apr 09 '15 at 11:15
  • @lapinkoira Thank you for your reply. Those are the variables which django defines, but what if i have custom variable like `MYVARIABLE = 'xyz'` and I change this variable value, will it cause problem? – Dalon Apr 09 '15 at 11:43
  • You could do that but it would be overwritten and not suggested. I had to do something similar and I did it using site settings models in a DB related to customers, etc – lapinkoira Apr 09 '15 at 11:45

1 Answers1

2

That's not good approach. After you restart or update your server your updated setting will be overwritten for variable that was defined in settings.

You can make own settings model with changeable settings and use it or get some config-apps like django-constance or django-solo or something else and change them legally.

amureki
  • 773
  • 1
  • 8
  • 15
  • Thank you. That is a good point, when i restart the server I may lose the values. Can you tell me, how can i set a few custom settings variables from another file, which can be loaded when server starts? – Dalon Apr 09 '15 at 11:48
  • @Dalon why do you want load something from file? Why don't use database for settings? And when you will store settings in db, you can easily change them manually, or from code, or from management commands, that will parse some files if you want so. – amureki Apr 10 '15 at 07:16
  • Actually i'm planing to do the same thing, store it in db and then retrieve it. Thank you for your input – Dalon Apr 13 '15 at 05:32
  • Can any one confirm if I have a valid use case for dynamic settings? i.e. it only stores data which is valid for the current uptime of the system - if there is a restart the data is "lost" which is acceptable (in this case) as it will be recreated and then store data applicable to the next period of uptime. – Derek Mar 27 '17 at 06:53