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)

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.1.tar.gz (291.5 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.1-py3-none-any.whl (294.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 720e94ee20c3ac4430daaef85058ea65b00db0b5020d637974e299f3fd21acf2
MD5 178ef5a14fc2912568eadb5698fccfe6
BLAKE2b-256 6084f5611b16ee1b0b624fbd559e334af2b63c0423d5d8b4e911f26ef2f80fc7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d59b7c3ccb268e66d27e6b231e42f39c2d1dbe2ec65c17110b2cab6dfacd65d0
MD5 0ea7e9cc6588599b4414ce30f42b75f0
BLAKE2b-256 fc2179d8fcc563ec935c3fc4c39f95d376ec179db53742cd485c8e2470b9b49b

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