Django JSONField

1.4.1 · active · verified Fri Apr 17

django-jsonfield provides a robust JSONField for Django models, offering functionality similar to Django's built-in JSONField (introduced in Django 3.1) but with compatibility for older Django versions (2.2+). It supports direct key lookups and queries on JSON data. The library is actively maintained with releases tied to Django compatibility updates and bug fixes. The current version is 1.4.1.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a model using `django-jsonfield.JSONField`, create instances with JSON data, update the data, and perform queries directly on the JSON fields. It includes a minimal Django setup for standalone execution.

import os
import django
from django.conf import settings
from django.db import models
from jsonfield.fields import JSONField # Correct import

# Minimal Django setup for standalone script
if not settings.configured:
    settings.configure(
        DEBUG=True,
        INSTALLED_APPS=[
            'myapp', # A dummy app for models
        ],
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': ':memory:',
            }
        },
    )
django.setup()

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    metadata = JSONField(default=dict) # Use default=dict for easier creation

    class Meta:
        app_label = 'myapp' # Required when models are not in a formal app dir

    def __str__(self):
        return f"{self.name} (Metadata: {self.metadata})";

# Simulate migrations for the quickstart
try:
    from django.core.management import call_command
    from io import StringIO
    out = StringIO()
    call_command('makemigrations', 'myapp', stdout=out, stderr=out, verbosity=0)
    call_command('migrate', 'myapp', stdout=out, stderr=out, verbosity=0)
except Exception as e:
    print(f"Warning: Could not run Django migrations simulation: {e}. Attempting manual schema creation.")
    # Fallback for environments where call_command is tricky
    with django.db.connection.schema_editor() as schema_editor:
        schema_editor.create_model(MyModel)

# Create an instance
instance = MyModel.objects.create(
    name="Product A",
    metadata={'sku': 'P-001', 'dimensions': {'width': 10, 'height': 20}}
)
print(f"Created: {instance}")

# Update JSON data
instance.metadata['status'] = 'available'
instance.metadata['dimensions']['depth'] = 5
instance.save()
print(f"Updated: {instance}")

# Retrieve and access JSON data
retrieved = MyModel.objects.get(name="Product A")
print(f"Retrieved SKU: {retrieved.metadata['sku']}")

# Query JSON data directly (supported by django-jsonfield)
products_with_sku = MyModel.objects.filter(metadata__sku='P-001')
print(f"Found {products_with_sku.count()} product(s) with SKU P-001.")

products_wide = MyModel.objects.filter(metadata__dimensions__width__gt=5)
print(f"Found {products_wide.count()} product(s) with width > 5.")

view raw JSON →