python - 是否有 django 习惯用法在数据库中存储与应用程序相关的变量?

标签 python django variables persistence idioms

我对 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/

相关文章:

python - 寻找ctypes unicode处理的 "Hello World"(包括Python和C代码)

python - 以完全错误的顺序打印代码

python - 作业不是由 APScheduler 的 BackgroundScheduler 执行的

python - 使用条件语句的 Pandas 样式

python - 如何验证两个不同的 .csv 文件列 id 是否与 python 匹配?

django - 事务管理的 block 以挂起的 COMMIT/ROLLBACK 结束

python - 在同一 View 中使用 django 查询的数据

php - 将值保存为数据库中的变量名

javascript - 具有字符串值的 PHP 变量与来自 javascript 的具有 html id 值的 PHP 变量不相等?

variables - 将变量从 Octopus 项目导出到 json 文件