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, 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).

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.0.tar.gz (290.3 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.0-py3-none-any.whl (292.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c2a2a2b537353e3207a3b96755363f2c1c327e1bbe5eb7be202ee3cc4f44636a
MD5 dae89e64fee90d856d346900e2e98d5c
BLAKE2b-256 381b279b53493df9fb5ef3aa8940f9b4e32731dd456901ab9ec1fdbb02cde077

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_jsonfield_formatter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f16cd80f87874d3d506987ac86b12158e9dd3359ba1cf44931cc55be6d6205f4
MD5 0e96d95d823ae00506a5bed4c8880d19
BLAKE2b-256 b8ff4a5a8db5b995ada0fb9c109f6c9f4b7f5406d08e8d65ffa2ddb8e7b8a83a

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