Skip to main content

Stores a model as a dictionary

Project description

django-modeldict

https://img.shields.io/pypi/v/django-modeldict-yplan.svg https://travis-ci.org/YPlan/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

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'

History

Pending release

  • New release notes here

1.5.3 (2016-09-20)

  • Stop rounding time.time() down to the nearest integer, so we are more fine grained around expiration. It might also fix a subtle timing bug around re-fetching the remote cache unnecessarily.

1.5.2 (2016-07-31)

  • Fixed update missing when _local_last_updated could be set even when it wasn’t updated

  • Fixed update missing from integer rounding in time comparison

  • Fixed CachedDict.__repr__ so it works for other subclasses of CachedDict than ModelDict (don’t assume self.model exists)

1.5.1 (2016-06-13)

  • Fixed local cache never expiring if value was checked too often.

  • Use Django’s cache.set_many for more efficient storage.

1.5.0 (2016-01-11)

  • Forked by YPlan

  • Fixed concurrency TOCTTOU bug for threaded Django servers.

  • Stopped including the ‘tests’ directory in package

  • Django 1.8 and 1.9 supported.

  • Python 3 support added.

  • Fixed setdefault() to return the value that was set/found, as per normal dict semantics. Thanks @olevinsky.

1.4.1 (2012-12-04)

  • Last release by Disqus

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-modeldict-yplan-1.5.3.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

django_modeldict_yplan-1.5.3-py2.py3-none-any.whl (13.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-modeldict-yplan-1.5.3.tar.gz.

File metadata

File hashes

Hashes for django-modeldict-yplan-1.5.3.tar.gz
Algorithm Hash digest
SHA256 ec67aa9bf6e689b3d468a770e38b90dd4b4b4ccb7ddd034d041836924b910eb6
MD5 46467a6b39f9ccfac49a0708cda3b161
BLAKE2b-256 5f0aa966b1d10546465b66728e0e4ce4c5908442a60fdd456e396692bdbf8bc6

See more details on using hashes here.

File details

Details for the file django_modeldict_yplan-1.5.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_modeldict_yplan-1.5.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b6144e9cb5cb03c206290ab73d954975387dc01ffb6bad2cfa06af06d9816dc0
MD5 4275fc5213ffe91fb866b13bca1fd68a
BLAKE2b-256 827d89fc713b7e535e3b1a0857d86246d5a9c14e3847dbe5851f7a04d8c8d67c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page