Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a django idiom to store app-related variables in the DB?

I'm quite new to django, and moved to it from 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:

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.

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.

(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).

like image 410
mac Avatar asked Apr 09 '12 14:04

mac


1 Answers

There are apps to achieve this, but I'd like to recommend django-modeldict from disqus, as its brief

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).

like image 137
okm Avatar answered Sep 21 '22 18:09

okm