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.

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.3.tar.gz (292.2 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.3-py3-none-any.whl (294.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6efe4386a658c5dfcdbb7d627b2d5bb6467be4f1646c4f6768c1ccee8e0d68f9
MD5 b3333e545852a88a5d31433d9726566d
BLAKE2b-256 161010ea4612b2e434c7d3e3982247cd22ee44f2a1f56015cb25402cc3d9ec2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 067fc79cd5b0bd326cd59f26a1c04e926b5bd3784a50829c5dca05216ae9e588
MD5 dc81ab22263d4521f2149dc04205b286
BLAKE2b-256 3d526eeb8e0245d406e8c4725cac709fed2465b18bab0cede67de32c25c9b8ae

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