Skip to main content

A widget for django's JSONField using the latest-and-greatest Json Editor

Project description

PyPI version Documentation Status Code style: black pre-commit

django-svelte-jsoneditor

A plug in widget for django's JSONField that allows manipulation and display of JSON data. The widget is built using Jos deJong's new svelte-jsoneditor.

This app is a replacement for django-jsoneditor (which uses a deprecated version of the widget, jsoneditor - you can see the differences here). You can read about why we're not directly contributing to django-jsoneditor here, the two projects might merge in the future.

Documentation

Installation

To get an application installed in your project, you need to add django_svelte_jsoneditor into INSTALLED_APPS in settings.py file.

# settings.py

INSTALLED_APPS = [
    # Other Django applications
    "django_svelte_jsoneditor",
]

Usage

The application contains new widget called SvelteJSONEditorWidget which adds editor capabilities to JSON fields in Django. Below you can see an example, of how to override the default widget for the JSON field (in this case textarea widget).

# admin.py

from django.contrib import admin
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget

from .models import MyModel


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

Another example is how to create a new Django form integrating SvelteJSONEditorWidget replacing Textarea widget.

# forms.py
from django import forms
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget


class MyJSONEditorForm(forms.Form):
    json = forms.JSONField(widgets=SvelteJSONEditorWidget())

Global settings

The application allows modifying some properties of svelte-jsoneditor inside the settings.py configuration file in Django. Official documentation is available on svelte-jsoneditor GitHub page. Note: Not all options are configurable which are provided by svelte-jsoneditor.

# settings.py

SVELTE_JSONEDITOR = {
    "mode": "tree",
    "mainMenuBar": True,
    "navigationBar": True,
    "statusBar": True,
    "askToFormat": True,
    "readOnly": False,
    "indentations": 4,
    "tabSize": 4,
    "escapeControlCharacters": False,
    "flattenColumns": True,
}

Available properties

Property Type Default
mode 'tree' or 'text' or 'table' 'tree'
mainMenuBar boolean True
navigationBar boolean True
statusBar boolean True
askToFormat boolean True
readOnly boolean False
indentations number or string 4
tabSize number 4
escapeControlCharacters boolean False
flattenColumns boolean True

Widget properties

SvelteJSONEditorWidget has additional argument called props which allows to override SVELTE_JSONEDITOR settings from settings.py.

# forms.py

from django import forms


class SvelteJsonEditorForm(forms.Form):
    my_json = forms.JSONField(widget=SvelteJSONEditorWidget(props={
        "readOnly": True
    }))

Custom widget properties in admin form

# admin.py

from django import forms
from django.contrib import admin

from .models import ExampleModel

class CustomForm(forms.ModelForm):
    class Meta:
        model = ExampleModel
        fields = "__all__"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["some_json_field"].widget = SvelteJSONEditorWidget(props={"readOnly": True})


@admin.register(ExampleModel, ExampleModelAdmin)
class ExampleModelAdmin(admin.ModelAdmin):
    form = CustomForm
    formfield_overrides = {
        models.JSONField: {
            "widget": SvelteJSONEditorWidget,
        }
    }

About Svelte

You don't need to know or care. It's the JavaScript framework used to develop the widget - but the widget JS is all pre-built so there are no extra requirements.

Developing

To get started with developing django-svelte-jsoneditor, fork the repo then open an environment in the devcontainer (the easiest way is to use GitHub codespaces or VSCode remote containers), then type:

python manage.py migrate
python manage.py createsuperuser
# (then enter user details for yourself)
python manage.py runserver
# (then go to the localhost address in your browser)
# (and in another terminal...)
pytest
# (this should run all tests and have them pass)

You'll find this takes you to the django admin where you have several example models registered, each of which use slightly different options on the json field, so you can see how the widget behaves.

On the subject of commit messages. It's helpful if you use the following conventional commit codes because then the PR and release notes get generated automatically!

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_svelte_jsoneditor-0.3.0.tar.gz (387.3 kB view details)

Uploaded Source

Built Distribution

django_svelte_jsoneditor-0.3.0-py3-none-any.whl (388.8 kB view details)

Uploaded Python 3

File details

Details for the file django_svelte_jsoneditor-0.3.0.tar.gz.

File metadata

File hashes

Hashes for django_svelte_jsoneditor-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c2400f8796e008682a422f850c77ba06498ca31117c3c4baefa0d7fe32f0e2f8
MD5 eb091378b873c33549ab2a2a4b14c805
BLAKE2b-256 1daefde95db8692ffb53332df548ec89c61d5e6959b832fa82b5d3758f7ef645

See more details on using hashes here.

File details

Details for the file django_svelte_jsoneditor-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_svelte_jsoneditor-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c884fef20642076b651273e6b4fee4f10926c560c4e3293d60a42ddf4116342
MD5 90d3be68cf771282a882d0c63fbb3f4b
BLAKE2b-256 cdc952c2780b79916d2e3d5869dd82650de074d5092948d6316762847951819e

See more details on using hashes here.

Supported by

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