Skip to main content

django-better-json-widget

Build Status codecov pypi downloads

Better JSON Widget for Django Admin

Features

  • Better Json widget with schema for your Django Admin site
  • Can watch for changes in the given field (follow_field) and dynamically update the JSON schema
  • Supports JSON Schema
  • You can edit generated UI fields specified in schema or edit raw JSON
  • Use Vue.js for UI
  • Supports Python 3.8+ and Django 3.2+

Limitations

  • Supports only a small subset of the JSON Schema (integer, number, boolean, string types)
  • Does not support enum, list types (yet) and nested objects (not planned)

So, PR's are welcome!

Installation

Install package

pip install django-better-json-widget

Add better_json_widget to your INSTALLED_APPS

Example

from better_json_widget.widgets import BetterJsonWidget
from django.contrib import admin
from django.forms import ModelForm

from .models import TestModel


schema_mapping = {
    "animal": {
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "properties": {
            "limbs": {
                "type": "integer",
                "title": "Number of limbs",
                "description": "How many limbs does the animal have?",
            },
            "color": {"type": "string", "title": "Color"},
            "herbivore": {
                "type": "boolean",
                "title": "Is it herbivore?",
                "default": True,
            },
        },
        "required": ["limbs", "herbivore"],
    },
    "superhero": {
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "title": "Name",
                "description": "Give a name to your superhero",
            },
            "superpower": {"type": "string"},
        },
        "required": ["name"],
    },
}

class TestModelAdminForm(ModelForm):
    class Meta:
        model = TestModel
        fields = "__all__"
        widgets = {
            "options": BetterJsonWidget(
                follow_field="type",
                # `schema_mapping` and `schema` can be callables
                schema_mapping=schema_mapping,
            ),
        }


@admin.register(TestModel)
class TestModelAdmin(admin.ModelAdmin):
    form = TestModelAdminForm
    fields = [
        "type",
        "options",  # JsonField
    ]

Also, if you don't need to dynamically change schema, you can use schema option:

BetterJsonWidget(
    schema={
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "type": "object",
        "properties": {
            ...
        },
        "required": [],
    },
)

Settings

If for some reason you don't want to use bundled Vue.js, you can change BETTER_JSON_WIDGET_VUE_URL settings:

BETTER_JSON_WIDGET_VUE_URL = "https://unpkg.com/vue@3"

If you set this setting to None, then bundled Vue.js will not be used.

TODO

  • Improve JSON Schema support
  • Show current field value in UI
  • UI tests

License

MIT

Credits

This project was generated with yakimka/cookiecutter-pyproject.

Metadata

Release files for django-better-json-widget 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-better-json-widget 0.4.0
File Size Uploaded Provenance
django-better-json-widget-0.4.0.tar.gz 57.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-better-json-widget 0.4.0
File Interpreter ABI Platform
django_better_json_widget-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 114.1 kB

Release files / django-better-json-widget-0.4.0.tar.gz

Download URL django-better-json-widget-0.4.0.tar.gz
Size 57.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1e0954fd785c61b7a62ee940d19440d2582eab532ab43736cf4f6e1c4025ae5e
BLAKE2b-256 checksum
How to use checksums
2cac28a09a15c9c37c4a1f2e9e68195bfa58aa42ff8747ed163fb6561100b6c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.5 Linux/5.13.0-1031-azure

Release files / django_better_json_widget-0.4.0-py3-none-any.whl

Download URL django_better_json_widget-0.4.0-py3-none-any.whl
Size 56.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c49a67ccacaee7792d2bccd155f97e68b8fff2ebef5853baf638d7a86b00f75
BLAKE2b-256 checksum
How to use checksums
eb4c55b29aa3b698ef9fdd0590d10b6f3693020ced6b32c336f0cf5884cf8c39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.5 Linux/5.13.0-1031-azure

Security for django-better-json-widget 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page