I'm quite new to django, and moved to it from Drupal.
我对django很新,并从Drupal转移到它。
In Drupal is possible to define module-level variables (read "application" for django) which are stored in the DB and use one of Drupal's "core tables". The idiom would be something like:
在Drupal中可以定义模块级变量(读取django的“application”),它们存储在DB中并使用Drupal的“核心表”之一。这个成语会是这样的:
variable_set('mymodule_variablename', $value);
variable_get('mymodule_variablename', $default_value);
variable_del('mymodule_variablename');
The idea is that it wouldn't make sense to have each module (app) to instantiate a whole "module table" to just store one value, so the core provides a common one to be shared across modules.
这个想法是让每个模块(app)实例化一个完整的“模块表”只是存储一个值是没有意义的,因此核心提供了一个共同的模块共享。
To the best of my newbie understanding of django, django lack such a functionality, but - since it is a common pattern - I thought to turn to SO community to check if there is a typical/standard/idiomatic way that django devs use to solve this problem.
就我对django的新手的理解而言,django缺乏这样的功能,但是 - 因为它是一种常见模式 - 我想转向SO社区来检查是否存在django开发人员用来解决的典型/标准/惯用方式这个问题。
(BTW: the value is not a constant that I could put in a settings file. It's a value that should be refreshed daily, and should be read at each request).
(顺便说一句:该值不是我可以放入设置文件的常量。它是一个应该每天刷新的值,应该在每个请求时读取)。
3 个解决方案
#1
6
There are apps to achieve this, but I'd like to recommend django-modeldict from disqus, as its brief
有应用程序可以实现这一点,但我想推荐来自disqus的django-modeldict作为其简介
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).
ModelDict是一种非常有效的方式来存储数据库中的设置之类的东西。整个模型转换为字典(懒惰)并存储在缓存中。它只在需要时(在进程中和基于CACHE_BACKEND)时才会失效。
#2
2
Data that is not static is stored in a model. If you need to share data or functions between apps I have seen the convention of making a shared app, something like 'common'. This would house shared models, or utility functions.
非静态数据存储在模型中。如果您需要在应用程序之间共享数据或功能,我已经看到了制作共享应用程序的惯例,例如“常见”。这将包含共享模型或实用程序功能。
In the django projects I have seen the data is usually specific. The data you are storing should be in a model that is representative of that data, I would rather have an explicit model/object representing my data then a generic object that houses vastly different data.
在django项目中,我看到数据通常是具体的。您存储的数据应该在代表该数据的模型中,我宁愿有一个显式的模型/对象来表示我的数据,然后是一个包含截然不同的数据的通用对象。
If you are only defining 1 or two variables which are changed daily, perhaps just a key/value store like memcached would work for you?
如果您只定义每天更改的一个或两个变量,那么像memcached这样的键/值存储可能对您有用吗?
#3
0
Another +1 for ModelDict. Another potential, similar solution is Django Constance:
ModelDict的另一个+1。另一个潜在的类似解决方案是Django Constance:
https://github.com/jazzband/django-constance
It's meant to store app config parameters in the database and has the advantage that it exposes a nice backend to edit them for administrators (with the right permissions), handles default values and also has caching etc.
它的目的是将应用程序配置参数存储在数据库中,并且具有以下优点:它为管理员公开了一个很好的后端(具有正确的权限),处理默认值并具有缓存等。
EDIT:
In case it's not clear from the documentation (which it isn't), you can set settings the same the 'Pythonic way.' I.e. to set a setting to a value, you do
如果文档中不清楚(不是这样),您可以将设置设置为'Pythonic方式'。即你可以将设置设置为值
from constance import config
来自constance import config
config.variable_name = value
config.variable_name = value
#1
6
There are apps to achieve this, but I'd like to recommend django-modeldict from disqus, as its brief
有应用程序可以实现这一点,但我想推荐来自disqus的django-modeldict作为其简介
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).
ModelDict是一种非常有效的方式来存储数据库中的设置之类的东西。整个模型转换为字典(懒惰)并存储在缓存中。它只在需要时(在进程中和基于CACHE_BACKEND)时才会失效。
#2
2
Data that is not static is stored in a model. If you need to share data or functions between apps I have seen the convention of making a shared app, something like 'common'. This would house shared models, or utility functions.
非静态数据存储在模型中。如果您需要在应用程序之间共享数据或功能,我已经看到了制作共享应用程序的惯例,例如“常见”。这将包含共享模型或实用程序功能。
In the django projects I have seen the data is usually specific. The data you are storing should be in a model that is representative of that data, I would rather have an explicit model/object representing my data then a generic object that houses vastly different data.
在django项目中,我看到数据通常是具体的。您存储的数据应该在代表该数据的模型中,我宁愿有一个显式的模型/对象来表示我的数据,然后是一个包含截然不同的数据的通用对象。
If you are only defining 1 or two variables which are changed daily, perhaps just a key/value store like memcached would work for you?
如果您只定义每天更改的一个或两个变量,那么像memcached这样的键/值存储可能对您有用吗?
#3
0
Another +1 for ModelDict. Another potential, similar solution is Django Constance:
ModelDict的另一个+1。另一个潜在的类似解决方案是Django Constance:
https://github.com/jazzband/django-constance
It's meant to store app config parameters in the database and has the advantage that it exposes a nice backend to edit them for administrators (with the right permissions), handles default values and also has caching etc.
它的目的是将应用程序配置参数存储在数据库中,并且具有以下优点:它为管理员公开了一个很好的后端(具有正确的权限),处理默认值并具有缓存等。
EDIT:
In case it's not clear from the documentation (which it isn't), you can set settings the same the 'Pythonic way.' I.e. to set a setting to a value, you do
如果文档中不清楚(不是这样),您可以将设置设置为'Pythonic方式'。即你可以将设置设置为值
from constance import config
来自constance import config
config.variable_name = value
config.variable_name = value