如何将 Django 应用程序设置保留在数据库中,或即时更改?
有人对如何创建一个允许我动态更改设置的系统有建议吗?例如,我有一个用户可以填充的文本区域,目前它的最大字符数限制为 1000。有一天我可能想将其更改为 500、2000 或其他值。所以我不想对模型本身施加限制,以免我想经历备份数据库、运行syncdb、重新导入数据库的噩梦,这样的噩梦只是为了改变一个简单的值。我宁愿通过 Javascript 或views.py 代码检查字符长度。
if (length > value_stored_somewhere)
throw error message
我想我可以将这些值放在 settings.py 中,或者可能是单独的 local-settings.py 中,但这意味着如果我想更改该值,我必须重新启动应用程序,对吧?或者我可能是错的,所以请告诉我我是否错了。
我认为执行此操作的理想方法是将设置放入数据库中,以便管理员可以即时更改值,而无需触及任何 .py 文件。我查看了 django-dbsettings 但那里似乎根本没有任何活动。
请给我建议和解决方案。多谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的所有假设都是对的。如果您想更改设置(在任何设置文件中),您需要重新启动应用程序。目前还没有办法“即时更改设置”。有很多可重复使用的应用程序可以处理所谓的实时设置。也许你可以尝试其中一种。请记住,即使您动态更改设置,也不会更改 Django 中的行为(因为 Django 或其他应用程序直接使用内部设置)。但对于自定义应用程序或项目来说,这是一个很好的方法。
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.