Skip to main content

Django JSON form field on steroids using react-json-schema-form

Project description

https://badge.fury.io/py/django-reactive.svg https://github.com/tyomo4ka/django-reactive/workflows/CI/badge.svg?branch=master https://codecov.io/gh/tyomo4ka/django-reactive/branch/master/graph/badge.svg

django-reactive integrates react-jsonschema-form (RJSF) in Django projects.

Motivation

JSON types is a cool feature of Postgres that allows combining both relational and non-relational approaches to storing data together. In most cases it would lead to a simpler database design.

The JSONField in Django provides a nice way of integrating json and jsonb Postgres types inside the ORM. ORM can even utilise JSON fields to perform database queries. jsonb type also offers indexing for JSON documents. That makes it a powerful tool in the application design and opens a wide range of use cases, e.g. polymorphic behaviour, storing complex hierarchies and lists of related entities.

However, the main limitation of JSONField in Django is that it does not offer a good way of managing such objects in the default admin UI. Defining JSON objects inside the textarea is not practical for most use cases. django-reactive tries to address this problem by offering an integration between JSONField and the awesome react-jsonschema-form (RJSF) JavaScript library. It also uses Python jsonschema <https://github.com/Julian/jsonschema> library for backend validation. Such integration from our practice can significantly reduce an amount of work you need to do to build custom forms for JSONField types.

In most cases developers would only need to define a JSON schema configuration for such field and optionally UI schema to modify UI for those forms.

A simple use case is demosntrated below:

from django.db import models

from django_reactive.fields import ReactJSONSchemaField


class TestModel(models.Model):
    simple = ReactJSONSchemaField(
        help_text='Simple',
        schema={
            "title": "A registration form",
            "description": "A simple form example.",
            "type": "object",
            "required": [
                "firstName",
                "lastName"
            ],
            "properties": {
                "firstName": {
                    "type": "string",
                    "title": "First name"
                },
                "lastName": {
                    "type": "string",
                    "title": "Last name"
                },
                "age": {
                    "type": "integer",
                    "title": "Age"
                },
                "bio": {
                    "type": "string",
                    "title": "Bio"
                },
                "password": {
                    "type": "string",
                    "title": "Password",
                    "minLength": 3
                },
                "telephone": {
                    "type": "string",
                    "title": "Telephone",
                    "minLength": 10
                }
            }
        },
        ui_schema={
            "firstName": {
                "ui:autofocus": True,
                "ui:emptyValue": ""
            },
            "age": {
                "ui:widget": "updown",
                "ui:title": "Age of person",
                "ui:description": "(earthian year)"
            },
            "bio": {
                "ui:widget": "textarea"
            },
            "password": {
                "ui:widget": "password",
                "ui:help": "Hint: Make it strong!"
            },
            "date": {
                "ui:widget": "alt-datetime"
            },
            "telephone": {
                "ui:options": {
                    "inputType": "tel"
                }
            }
        },
    )

It will generate a form like this:

images/simple.png

Quick start

Install django-reactive:

pip install django-reactive

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'django_reactive.apps.DjangoReactJsonSchemaFormConfig',
    ...
)

Run example app

  • docker-compose up

  • poetry install

  • ./example/manage.py makemigrations

  • ./example/manage.py migrate

  • ./example/manage.py createsuperuser

  • ./example/manage.py runserver

  • open http://127.0.0.1:8000/admin

  • Go to “Test model” to check a few examples

Features

  • React, RJSF and other JS assets are bundled with the package.

  • Integration with default Django admin theme.

  • Backend and frontend validation.

Limitations

To implement this behaviour you can define an array schema with one property serving as a key of the object and do transformation in your JSON class. An example will be provided later.

Future development

  • Display description as tooltips

  • Polish styles and HTML generated by RJSF

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-reactive-0.0.5.tar.gz (355.4 kB view details)

Uploaded Source

Built Distribution

django_reactive-0.0.5-py3-none-any.whl (358.3 kB view details)

Uploaded Python 3

File details

Details for the file django-reactive-0.0.5.tar.gz.

File metadata

  • Download URL: django-reactive-0.0.5.tar.gz
  • Upload date:
  • Size: 355.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for django-reactive-0.0.5.tar.gz
Algorithm Hash digest
SHA256 88abe8eaaf6a920f3bd14c3098c25cd47f33839bba9d48927fef7ccfbef5c0d8
MD5 722e9a8c76dbc126ab361b089c15d569
BLAKE2b-256 e20fc784b029b66f901fdb5d54afe9559f4ad55111eabae6821fa1fa10842345

See more details on using hashes here.

File details

Details for the file django_reactive-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: django_reactive-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 358.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for django_reactive-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a4ff1ba37add44ef05d70f09bbb331be3f710dd1f13ce367ee5ea98a376c0e37
MD5 f03417be6c97a55143062db8bc0528e6
BLAKE2b-256 4eb9003248ceebc530609e2dd725c2c3de861c52a6fac25c99e0d352975a895c

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