Skip to main content

Generic JSON model and form fields.

Project description

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:

{"date": "2012-04-23T19:16:54.133", "num": "1.2399"}

And also provides safe access to the datetime module for explicit use:

{"date": datetime.datetime(2012, 4, 23, 19, 16, 54, 133000)}

While the JSON string will not be deserialized until it is accessed it can still be a performance concern, so you may find it valuable to disable the custom deserializer (JSONField(decoder=None)).

django-json-field is also compatible with South.

Installation

First, install the dateutil library:

pip install python-dateutil

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 "null" if not provided and null=False, otherwise None

  • db_type: Allows you to specify the column type (default: text)

  • encoder: Custom JSON encoder (default: DjangoJSONEncoder)

  • decoder: Custom JSON decoder (default: json_field.fields.JSONDecoder)

  • encoder_kwargs: Specify all arguments to the encoder (overrides encoder)

  • decoder_kwargs: Specify all arguments to the decoder (overrides decoder)

  • evaluate_formfield: Evaluate (risky) and enable use of the datetime module in the form field (default: False)

License

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

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-json-field-0.5.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file django-json-field-0.5.tar.gz.

File metadata

File hashes

Hashes for django-json-field-0.5.tar.gz
Algorithm Hash digest
SHA256 c5905f71c4ad64f783fdb3fcfbddcb22ff8bb131268dcc360bfae8cf1f181b71
MD5 7fc9c840d1aaba8f184bb143b5af5e69
BLAKE2b-256 f31ebac80bdba36fcd5a056c0b2021e4b8735ad706578a3006a2d87d4ad40043

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