我对 django 很陌生,是从 Drupal 转到它的。
在 Drupal 中,可以定义存储在数据库中并使用 Drupal 的“核心表”之一的模块级变量(django 的“应用程序”)。成语是这样的:
variable_set('mymodule_variablename', $value);
variable_get('mymodule_variablename', $default_value);
variable_del('mymodule_variablename');
这个想法是让每个模块(应用程序)实例化整个“模块表”以仅存储一个值是没有意义的,因此核心提供了一个通用的值以在模块之间共享。
就我对 django 的新手理解而言,django 缺乏这样的功能,但是 - 因为它是一种常见的模式 - 我想求助于 SO 社区来检查是否有典型/标准/惯用的方式django 开发人员用来解决这个问题。
(顺便说一句:该值不是我可以放入设置文件中的常量。它是一个应该每天刷新的值,并且应该在每次请求时读取)。
最佳答案
有应用程序可以实现这一点,但我想推荐 django-modeldict来自 disqus,作为其简介
ModelDict is a very efficient way to store things like settings in your database. The entire model is transformed into a dictionary (lazily) as well as stored in your cache. It's invalidated only when it needs to be (both in process and based on CACHE_BACKEND).
关于python - 是否有 django 习惯用法在数据库中存储与应用程序相关的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10074502/