Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adamchainz/django-modeldict
Stores a model as a dictionary
https://github.com/adamchainz/django-modeldict
Last synced: 3 months ago
JSON representation
Stores a model as a dictionary
- Host: GitHub
- URL: https://github.com/adamchainz/django-modeldict
- Owner: adamchainz
- License: apache-2.0
- Archived: true
- Fork: true (disqus/django-modeldict)
- Created: 2014-09-18T15:25:17.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-05-17T20:52:59.000Z (over 5 years ago)
- Last Synced: 2024-05-01T12:53:24.126Z (7 months ago)
- Language: Python
- Homepage: https://pypi.python.org/pypi/django-modeldict-yplan
- Size: 176 KB
- Stars: 26
- Watchers: 5
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- License: LICENSE
Awesome Lists containing this project
README
================
django-modeldict
================.. image:: https://img.shields.io/pypi/v/django-modeldict-yplan.svg
:target: https://pypi.python.org/pypi/django-modeldict-yplan.. image:: https://travis-ci.org/adamchainz/django-modeldict.svg?branch=master
:target: https://travis-ci.org/adamchainz/django-modeldict**Retired: this project is no longer maintained.** I (Adam Johnson) no longer
have time to continue maintaining this. I was doing so to support
`gargoyle-yplan `__, a fork for my
ex-employer YPlan. If you'd like to sponsor ongoing maintenance or take it over
yourself, please contact [email protected].``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: 3.6
* Django: 1.11, 2.0, 2.1, 2.2Python 3.4+ supported.
Install
-------Install it with **pip**:
.. code-block:: bash
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
-------------.. code-block:: python
# 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'