How to keep Django app settings in database, or change on-the-fly?
Asked Answered
N

1

8

Does anyone have a suggestion on how to create a system that allows me to change settings on the fly? For example, I have a textarea that users can fill, and right now it has a maximum character limit of 1000. Someday I might want to change it to 500, or 2000, or something else. So I do not want to put a limit on the model itself, lest I want to go through the nightmare of backing up the DB, run syncdb, re-import DB, such a nightmare just to change a simple value. I'd rather do a check through Javascript or a views.py code on the character length.

if (length > value_stored_somewhere)
 throw error message

I thought I could just put these values in settings.py, or perhaps a separate local-settings.py, but that means if I want to change the value I would have to restart the app, right? Or I could be wrong, so please tell me if I am wrong.

I thought that the ideal way to do this would be to put the settings in the database, so administrators can change values on-the-fly without touching any .py files. I took a look at django-dbsettings but there doesn't seem to be any activity at all there.

Please give me suggestions and solutions. Thanks a lot!

Nuncle answered 25/4, 2011 at 2:59 Comment(0)
M
10

You are right with all your assumptions. You need to restart the app if you want to change settings (in any settings file). Right now there is no way to "change settings on the fly". There are a bunch of reusable apps outside handling so called Live Settings. Maybe you try one of these. Keep in mind that even you change settings on the fly you will not change behavior in Django (since Django or also other apps are using internal settings directly). For a custom app or project though, this is a good approach.

Minestrone answered 25/4, 2011 at 3:9 Comment(4)
Yikes... That's a scary answer. So what do Django pros do in this situation? How do they handle settings change? Also, on a live production environment using Apache how would I restart the Django app? Thanks!Nuncle
Yes, handling settings within django is not optimal and criticized already quite often. For small projects doing changes in the files and deploying them is fine, for bigger projects I would consider a live settings app right from the start (though in my company I wrote a custom one, since none of the one above fit our needs).Minestrone
And about your question: if you deploy a django project with apache and mod_wsgi you can simply do touch apache.wsgi (assuming your wsgi script for your project is apache.wsgi). This will force the underlying apache process to use the new code.Minestrone
Ok then I guess I will put some settings into settings.py or local-settings.py. Thanks!Nuncle

© 2022 - 2024 — McMap. All rights reserved.