3

In Django when I want to store models like users, places and so on I may use an SQL database, but when I want to store a particular value (only one) I have no idea what to use.

So, I'm doing a website where there is some information that changes monthly. Giving one example:

<meta name="description" content="This team is in {{ ranking }} in the world ranking"> 

I'll need to change that value, ideally from Django Admin monthly and a Model with one object seems too much (not too much work but simply too much).

I started looking at NoSQL and from this question I'm inclined to use Redis or a similar key-value DB.

Is it the right approach or am I completely off track?

Community
  • 1
  • 1
NBajanca
  • 3,544
  • 3
  • 26
  • 53
  • SQLite gives you room to grow and is super-lightweight (as the name implies) This is really kind of a question likely to attract opinion-based answers, though. – sytech Oct 27 '16 at 01:42
  • 1
    "a Model with one object seems too much (not too much work but simply too much)." I don't see why it is. You create it, it stores a tiny bit in your database and that's it; it won't exhaust your disk space or anything. You're overthinking it; just keep it simple and use what you know. –  Oct 27 '16 at 01:43
  • 1
    in addition to the comment of @Evert I'd like to add that writing a simple key value store model is done quicker than writing this question. – Klaus D. Oct 27 '16 at 03:18
  • A key-value table, cached in redis is more than enough! – Mohammad Jafar Mashhadi Oct 27 '16 at 10:05

2 Answers2

2

You could use python's shelve for a simple out-of-the-box solution for object persistence. It's easy to use:

with shelve.open('my_store') as my_store:
    my_store['ranking'] = 10

and then:

with shelve.open('my_store') as my_store:
    ranking = my_store['ranking']

Hope this helps!

damores
  • 2,251
  • 2
  • 18
  • 31
  • Thanks for the solution but it only fits some cases so I accepted the other answer. For ex: It wouldn't work with replication of servers. – NBajanca Nov 13 '16 at 20:05
1

I think for this case it better to use existing solutions like Constance - Dynamic Django settings. It is general solution for modifiable variables through django admin interface.

Here is list of similar apps https://djangopackages.org/grids/g/live-setting/

Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63