Django JSONField
A reusable Django model field that allows you to store validated JSON, automatically handling serialization to and from the database. The package is currently at version 3.2.0. This package is in maintenance mode and is considered deprecated in favor of Django's native JSONField (introduced in Django 3.1) which offers superior database-agnostic support and querying capabilities.
Warnings
- deprecated The `jsonfield` package is deprecated. Django 3.1 and newer versions provide a native `django.db.models.JSONField` that is recommended for all new projects and migrations. The native field offers better database-agnostic support and enhanced querying capabilities, especially with PostgreSQL's JSONB.
- breaking Version 3.0.0 introduced significant breaking changes, including dropping support for Django versions older than 2.0 and Python versions older than 3.5. It also reworked field serialization/deserialization, moved form fields (e.g., `JSONFormField` was renamed to `forms.JSONField` and moved), and removed South migration support.
- breaking Version 1.0.0 (and related pre-1.0.0 releases) introduced a breaking change by removing direct native PostgreSQL JSON data type support. If your project relied on PostgreSQL's native JSON features with `jsonfield` versions prior to 1.0.0, upgrading to 1.0.0+ can be a breaking change as data might subsequently be stored as plain text.
- gotcha There are multiple Python packages on PyPI that are named 'jsonfield' or 'django-jsonfield'. This entry specifically refers to the `jsonfield` package (on PyPI, `rpkilby/jsonfield` on GitHub, formerly `bradjasper/django-jsonfield`). Be cautious not to confuse it with other packages, such as `django-jsonfield` (on PyPI, `adamchainz/django-jsonfield` on GitHub).
- gotcha While `jsonfield` stores JSON, its querying capabilities are limited to basic text lookups (e.g., `exact`, `regex`) because values are stored as serialized JSON strings. It is not designed to provide advanced, database-native JSON querying functionalities like those offered by `django.db.models.JSONField` (especially when used with PostgreSQL's JSONB type).
Install
-
pip install jsonfield
Imports
- JSONField
from jsonfield import JSONField
Quickstart
from django.db import models
from jsonfield import JSONField
class MyModel(models.Model):
name = models.CharField(max_length=100)
data = JSONField(default=dict) # Use a callable for mutable defaults
# Example usage:
# obj = MyModel.objects.create(name='Example', data={'key': 'value', 'number': 123})
# print(obj.data['key'])