A reusable Django field that allows you to store validated JSON in your model.
Project description
django-jsonfield
django-jsonfield is a reusable Django field that allows you to store validated JSON in your model.
It silently takes care of serialization. To use, simply add the field to one of your models.
Python 3 & Django 1.11 through 3.0 supported!
Use PostgreSQL? 1.0.0 introduced a breaking change to the underlying data type, so if you were using < 1.0.0 please read https://github.com/dmkoch/django-jsonfield/issues/57 before upgrading. Also, consider switching to Django’s native JSONField that was added in Django 1.9.
Note: There are a couple of third-party add-on JSONFields for Django. This project is django-jsonfield here on GitHub but is named jsonfield on PyPI. There is another django-jsonfield on Bitbucket, but that one is django-jsonfield on PyPI. I realize this naming conflict is confusing and I am open to merging the two projects.
Note: Since version 1.9, Django added native PostgreSQL JSON support in django.contrib.postgres.fields.JSONField. This module is still useful if you need to support JSON in databases other than PostgreSQL or are creating a third-party module that needs to be database-agnostic. But if you’re an end user using PostgreSQL and want full-featured JSON support, I recommend using the built-in JSONField from Django instead of this module.
Note: Semver is followed after the 1.0 release.
Installation
pip install jsonfield
Usage
from django.db import models
from jsonfield import JSONField
class MyModel(models.Model):
json = JSONField()
Advanced Usage
By default python deserializes json into dict objects. This behavior differs from the standard json behavior because python dicts do not have ordered keys.
To overcome this limitation and keep the sort order of OrderedDict keys the deserialisation can be adjusted on model initialisation:
import collections
class MyModel(models.Model):
json = JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict})
Other Fields
jsonfield.JSONCharField
If you need to use your JSON field in an index or other constraint, you can use JSONCharField which subclasses CharField instead of TextField. You’ll also need to specify a max_length parameter if you use this field.
Compatibility
django-jsonfield aims to support the same versions of Django currently maintained by the main Django project. See Django supported versions, currently:
Django 1.11 (LTS) with Python 2.7, 3.5, or 3.6
Django 2.2 (LTS) with Python 3.6, 3.7, or 3.8
Django 3.0 with Python 3.6, 3.7, or 3.8
Testing django-jsonfield Locally
To test against all supported versions of Django:
$ docker-compose build && docker-compose up
Or just one version (for example Django 3.0 on Python 3.8):
$ docker-compose build && docker-compose run tox tox -e py38-dj30
Travis CI
Contact
Twitter: @bradjasper
Email: contact@bradjasper.com
Changes
Take a look at the changelog.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jsonfield-2.1.0.tar.gz
.
File metadata
- Download URL: jsonfield-2.1.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.42.1 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11fd69f83a7f44de749f91beea0d41871a893178cdb8ff5564cfeb3e12d7ff9e |
|
MD5 | 722d118a3b32196f83fe619f6440c024 |
|
BLAKE2b-256 | fcf52761cd46f61172e158344d5fbdee2311858f1dbc75250051c6b1aac2189a |
File details
Details for the file jsonfield-2.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: jsonfield-2.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.42.1 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cded15ff4ae9b77cceb93a6662ecef8fb2baa66f2756ca0e0e02a96fdd527344 |
|
MD5 | 66a79a44292d850bffcfb4fa820be42a |
|
BLAKE2b-256 | 08ec801c358bf8267e520814d57ac8c7743a73767ef96c3d27335b0c60b5c812 |