Coder Social home page Coder Social logo

django-jsonfield's Introduction

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.8 through 1.11 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 rpkilby#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: Django 1.9 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.8 (LTS) with Python 2.7, 3.3, 3.4, or 3.5
  • Django 1.9 with Python 2.7, 3.4, or 3.5
  • Django 1.10 with Python 2.7, 3.4, or 3.5
  • Django 1.11 (LTS) with Python 2.7, 3.4, 3.5 or 3.6

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 1.10 on Python 3.5):

$ docker-compose build && docker-compose run tox tox -e py35-1.10

Travis CI

https://travis-ci.org/dmkoch/django-jsonfield.svg?branch=master

Contact

Web: http://bradjasper.com

Twitter: @bradjasper

Email: [email protected]

Changes

Take a look at the changelog.

django-jsonfield's People

Contributors

adam-iris avatar admackin avatar bkvirendra avatar bradjasper avatar bruth avatar carljm avatar cmaring avatar danostrowski avatar decentral1se avatar diwu1989 avatar dmkoch avatar fletom avatar gaelen avatar gagaro avatar gitaarik avatar jarodl avatar jasonwmiller avatar jayfk avatar jrief avatar kr0ntab avatar l0kix2 avatar lambacck avatar mmautner avatar mwaterfall avatar nicholasserra avatar paltman avatar pelme avatar ross avatar sobolevn avatar yannik-ammann avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.