Stores a model as a dictionary
Clone or download
Pull request Compare This branch is 169 commits ahead, 3 commits behind disqus:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
modeldict
tests
.editorconfig
.gitignore
.travis.yml
HISTORY.rst
LICENSE
MANIFEST.in
README.rst
requirements.in
requirements.txt
runtests.py
setup.cfg
setup.py
tox.ini

README.rst

django-modeldict

https://travis-ci.org/adamchainz/django-modeldict.svg?branch=master

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

It was originally created by Disqus, but due to the inactivity we at YPlan have taken over maintenance on this fork.

Requirements

Tested with all combinations of:

  • Python: 2.7, 3.5
  • Django: 1.8, 1.9, 1.10, 2.0, 2.1

Install

Install it with pip:

pip install django-modeldict-yplan

Make sure you pip uninstall django-modeldict first if you're upgrading from the original to this fork - the packages clash.

Example Usage

# You'll need a model with fields to use as key and value in the dict
class Setting(models.Model):
    key = models.CharField(max_length=32)
    value = models.CharField(max_length=200)

# Create the ModelDict...
settings = ModelDict(Setting, key='key', value='value', instances=False)

# And you can treat it like a normal dict:

# Missing values = KeyError
settings['foo']
>>> KeyError

# Sets supported
settings['foo'] = 'hello'

# Fetch the current value using normal dictionary access
settings['foo']
>>> 'hello'

# ...or by normal model queries
Setting.objects.get(key='foo').value
>>> 'hello'