Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Generic JSON model and form fields.
Python
Tag: 0.1

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
json_field
test_project
.gitignore
AUTHORS
LICENSE
MANIFEST
MANIFEST.in
README.rst
manage.py
setup.py

README.rst

Django JSON Field

django-json-field contains a flexible JSONField and associated form field. The model field is not only capable of serializing common JSON data types (int, float, decimal, string, time, date, datetime, etc.) but also lazily deserializing them so they can be accessed and modified as normal Python objects within Django.

A form field is also provided. It will accept serialized representations (i.e. {"date": "2012-04-23T19:16:54.133", "num": "1.2399999999999999911182158029987476766109466552734375"}) but also provides safe access to the datetime module and Decimal class for explicit use (i.e. {"date": datetime.datetime(2012, 4, 23, 19, 16, 54, 133000), "num": Decimal('1.2399999999999999911182158029987476766109466552734375')}).

While the JSON string will not be deserialized until it is accessed it can still be a performance concern. You may find it valuable to disable deserialization (JSONField(decoder=None)), or to defer loading the field altogether (i.e. MyModel.objects.all().defer('json')).

django-json-field is also compatible with South.

Installation

Install from PyPI:

pip install django-json-field

Install from GitHub:

git clone git://github.com/derek-schaefer/django-json-field.git

pip install -e git+git://github.com/derek-schaefer/django-json-field.git#egg=json_field

Configuration

Add json_field to your PYTHONPATH and INSTALLED_APPS setting:

INSTALLED_APPS = (
    ...
    'json_field',
    ...
)

That's all!

Usage

Add a JSONField to your model like any other field.

from json_field import JSONField
from django.db import models

class MyModel(models.Model):

    json = JSONField()

JSONField also has a few additional optional parameters.

  • default: Falls back on {} if not provided.
  • db_type: Allows you to specify the column type (default: text)
  • encoder: Custom JSON encoder (default: DjangoJSONEncoder)
  • decoder: Custom JSON decoder (default: json_fields.fields.JSONDecoder)
  • encoder_kwargs: Specify all arguments to the encoder (overrides encoder)
  • decoder_kwargs: Specify all arguments to the decoder (overrides decoder)

License

django-json-field is licensed under the New BSD license.

Something went wrong with that request. Please try again.