I need to store dynamic (will change every day/hour) value in django app. Value will be generated every day, and I will need access to this key:value from all views in the application. I don't want to store it in some model object instance, it should work like settings.py but I need to store it in database instead. Any ideas?
Asked
Active
Viewed 2,061 times
2
-
1If this is something that needs to persist, this is exactly what the database is for. Create a model for that data and store it. Update according to your needs. – msvalkon Jun 25 '14 at 11:57
-
1Do I really need new model to store ONE key:value pair? :( – dease Jun 25 '14 at 12:01
-
@msvalkon, Mere persistence is *not exactly* what a database is for. A RDBMS isn't some tree where you can bury your nuts for the winter; it's a truthmachine, that you model the Universe of Discourse in, so you can query it for truths about your universe, by inference using propositional logic and relational algebra. – Chris Wesseling Jun 25 '14 at 12:08
-
Of course you can serialize this data to a file using pickle but I don't see the point, it's just a different abstraction. – msvalkon Jun 25 '14 at 12:09
-
@ChrisWesseling let's not nitpick. Of course mere persistence is not the definition of an RDBMS, but it's one use case and it's perfectly good for *this particular one*. – msvalkon Jun 25 '14 at 12:13
-
@msvalkon that totally depends on the nature of the data. The hour changes every hour. But I wouldn't store that in a rdbms. Just because you have a hammer, doesn't make every hard iron pointy thing a nail. – Chris Wesseling Jun 25 '14 at 12:17
-
Storing that value in a database is probably the fastest and most sensible thing here, as the op wants to be able to modify the data from every view. Other options that I can think of quickly is to store this in a file (pickle, shelve), which would require explicit methods for locking the file during writing, or perhaps another key-value store like redis, which is just a silly idea for *one* key-value pair. Are there any other sensible solutions? – msvalkon Jun 25 '14 at 12:30
2 Answers
1
what about leveldb? https://code.google.com/p/leveldb/
LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
there is also python wrap https://code.google.com/p/py-leveldb/
or if you need distributed access checkout http://memcached.org/

okocian
- 488
- 4
- 11
0
Why not use a key-value datastore like Redis? You can use it as a cache backend or as a normal store, it's fast and will persist your data. https://django-redis.readthedocs.org/en/latest/ https://pypi.python.org/pypi/redis/2.10.1

bakkal
- 54,350
- 12
- 131
- 107
-
It looks little overwhelming for my small needs. I need to store one or two key:value pairs only. – dease Jun 25 '14 at 12:13