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.2.tar.gz (291.9 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.2-py3-none-any.whl (294.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 91f9e502223297520a3c1fec5af6d54d64185452de7850a2522c6c8fab90ae2d
MD5 e4288c745230cde37ba913c4b20f33f1
BLAKE2b-256 d4e2975535382c6aa584f4c6b3bb9f7b3c606760876cf40ccd10c5278965ec73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 926a833c11aac686840e2962b31c05665f225b4ec035137d498a52075c3b620f
MD5 f4f242339d056850febe2ad8c09516ee
BLAKE2b-256 ab7d40b7c47da026704e03af49caea96dc3edad1a6b9c9c2cb15aafcf00207d5

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