4

Pieces of JSON Schema are easily used with frontend JavaScript libraries like Angular, React and Alpaca to create an html form with validation. This should also work with a Python solution django-jsonschema-form or django-schemulator but I am new to Django and having a lot of trouble working it out..

Here is a screenshot from the video from AlpacaJS which communicates easily what this is supposed to achieve:

JSON Schema becomes a form

I have done some testing with the two libraries above, and the former seems much better maintained and less buggy, the only one of the two in PyPI.

My directory tree created by Django 1.11.4 looks like this:

.
├── db.sqlite3
├── jschemaforms
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
├── myproject
│   ├── __init__.py
│   ├── settings.py
│   ├── templates
│   │   ├── base.html
│   │   └── includes
│   ├── urls.py
│   └── wsgi.py
└── README.md

The docs for django-jsonschema-form specify a class:

# Overriding widgets for all instances of JSONField on PageAdmin form
class PageAdmin(admin.ModelAdmin):
    formfield_overrides = {
        JSONField: {'widget': JSONSchemaWidget(schema)}
    }

Whereabouts in the directory tree is such a class supposed to go?

schema is the piece of json you feed it to define your form

I had a look at the various answers in this SO question to try to work it out:

Django Admin - Overriding the widget of a custom form field

It seems that forms.py and the ModelAdmin object are enough to implement this, but am not sure where in the tree the ModelAdmin object is supposed to live.

How can this be achieved with Django?

  1. rendering the form from jsonschema
  2. validating on client side what the user puts into the form against the jsonschema
  3. retrieving the data from the form POST request when the user clicks 'submit' as json

I am not sure if those two libraries are the answer..

cardamom
  • 6,873
  • 11
  • 48
  • 102
  • I do not know enough of json schema to answer. I could add my response as a comment here as it does not fully answer your question. – Ananth Nov 12 '18 at 09:27
  • Thanks for the answer.. Well, the thing I don't know enough about is Django. Have close to never used it, just done some tutorials so was hoping someone who actually knew something about it might respond. Not sure if the 2 libraries I mention are useful or useless for the purpose either.. – cardamom Nov 12 '18 at 09:30
  • (Preferably Django 1 but if the solution must be Django 2, will go with that) – cardamom Nov 12 '18 at 09:31
  • I know django well. I just don't have any idea what this json schema is. So can't help you there I'm afraid. – Ananth Nov 12 '18 at 10:44
  • About 4 years ago a [committee on the internet defined jsonschema](https://json-schema.org/specification-links.html) to specify schemas of json. Various libraries now let you validate a piece of json against its jsonschema. More recently, jsonschema has been abused by front end developers for form creation and validation. [There is an animation here](https://github.com/mozilla-services/react-jsonschema-form) (ignore the UISchema bit). Question is whether this can be done with a Python library rather than a JavaScript library. Don't stress about it though.. – cardamom Nov 12 '18 at 10:52
  • 2021: Using this mindset, I am leaning towards [django-ninja](https://django-ninja.rest-framework.com/) and the very young [djantic (ex django-pydantic)](https://jordaneremieff.github.io/djantic/) which are based on [pydantic](https://pydantic-docs.helpmanual.io/), intending to use [react-jsonschema-form](https://react-jsonschema-form.readthedocs.io/en/latest/). – raratiru May 07 '21 at 13:50

1 Answers1

1

The PageAdmin class goes in a file called admin.py, next to its corresponding model.py. Here are the Django 1.11 docs on discovering admin files.

You also have to register the admin for your model. You use the django.contrib.admin.register decorator for this. The same page has the docs for this decorator as well.

The usage goes something like

from somewhere import Page
from django.contrib import admin

# Overriding widgets for all instances of JSONField on PageAdmin form
@admin.register(Page)
class PageAdmin(admin.ModelAdmin):
    formfield_overrides = {
        JSONField: {'widget': JSONSchemaWidget(schema)}
    }

This code snippet registers your PageAdmin class as the ModelAdmin for your Page model.

Ananth
  • 848
  • 11
  • 26