Skip to main content

JSONEditor-based widget for Django admin JSONField with tree/code/view modes.

Project description

django-jsonfield-formatter

Installable Django app that replaces the default admin widget for JSONField with JSONEditor (tree, code, and view modes, search, navigation bar, and prettified editing).

Requirements

  • Python 3.10+
  • Django 4.2+

Install

pip install django-jsonfield-formatter

Or from a checkout:

pip install -e .

Setup

  1. Add the app to INSTALLED_APPS:
INSTALLED_APPS = [
    # ...
    "jsonfield_formatter",
]
  1. Run collectstatic in production so jsoneditor.min.js, jsoneditor.min.css, json_formatter_widget.css, and icons are available.

Usage in the admin

Option A — formfield_overrides

from django.contrib import admin
from django.db.models import JSONField

from jsonfield_formatter.widgets import JSONFormatterWidget


@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        JSONField: {"widget": JSONFormatterWidget},
    }

Option B — mixin

from django.contrib import admin

from jsonfield_formatter.admin import JSONFormatterAdminMixin


@admin.register(MyModel)
class MyModelAdmin(JSONFormatterAdminMixin, admin.ModelAdmin):
    pass

If you already use formfield_overrides, merge JSONField into your dict instead of using the mixin.

Widget options

JSONFormatterWidget accepts:

Argument Description
attrs Extra HTML attributes for the textarea (e.g. class).
mode Initial mode: tree, code, or view (default tree).
height CSS height for the editor area (default 400px).
editor_options Dict merged into JSONEditor’s options (see JSONEditor API).
show_fullscreen_button When True (default), shows Fullscreen so the editor can use the browser’s full screen; Escape exits.

Fullscreen

The widget adds a Fullscreen control above the editor. It calls the browser Fullscreen API on the editor panel so the JSON UI uses the whole display. JSONEditor is resized when entering or leaving fullscreen. To hide the button:

JSONFormatterWidget(show_fullscreen_button=False)

Saving

Before submit, the widget copies JSONEditor’s output into the hidden textarea. It prefers getText() (JSONEditor’s own JSON string) instead of parsing with get() and re-serializing with JSON.stringify, so large integers, key order, and other details are not altered by a JavaScript object round trip.

Root JSON null: Django’s JSONField turns the posted text "null" into Python None, which is usually wrong for default=dict or required fields. The widget coerces a root JSON null to {} before POST. If you need a real null root, set null=True on the model field and understand the admin may still normalize empty states to {} in edge cases—see CHANGELOG.md.

Example:

JSONFormatterWidget(
    mode="code",
    height="min(60vh, 520px)",
    editor_options={"sortObjectKeys": True},
)

Bundled JSONEditor version

Static files are vendored from jsoneditor 10.1.0 (see jsonfield_formatter/widgets.py).

Development

python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest

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_jsonfield_formatter-0.1.4.tar.gz (293.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_jsonfield_formatter-0.1.4-py3-none-any.whl (295.4 kB view details)

Uploaded Python 3

File details

Details for the file django_jsonfield_formatter-0.1.4.tar.gz.

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1a059fa0de183184b5af0cd3467340e37d5daa378e411b00485f1d342f86c723
MD5 69250c804e3d8972259cf062a2bb1a69
BLAKE2b-256 f5d7354ceee9fee9453804c0e03aa8ae11b0461c6efe4e247b0814eb56a4304a

See more details on using hashes here.

File details

Details for the file django_jsonfield_formatter-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0685068703170055a5ac5fd5524b369c9c6f77bf557b82f5e7aa79088f16194c
MD5 bc3e30012024fcbbbb2a51d311eedc90
BLAKE2b-256 3af2d5256b1a0611c10137d04f94809fa0816baf9fc94879c9dcd4a1999e3b5f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page