使用django-jsonschema-form或django-schemulator将jsonschema渲染为表单 [英] render jsonschema as a form with django-jsonschema-form or django-schemulator

查看:196
本文介绍了使用django-jsonschema-form或django-schemulator将jsonschema渲染为表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON模式的片段很容易与Angular,React和Alpaca等前端JavaScript库一起使用,以创建一个带有验证的html表单.这也应该与Python解决方案 django-jsonschema-form django-schemulator ,但是我对Django还是陌生的,在解决它时遇到了很多麻烦. >

这是来自 AlpacaJS 的视频的屏幕截图,可以轻松传达预期的效果:

我已经对上面的两个库进行了一些测试,并且前者似乎维护得更好并且没有太多错误,这是PyPI中两个库中的唯一一个.

我的Django 1.11.4创建的目录树如下所示:

.
├── 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

django-jsonschema-form的 docs 指定一个类:

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

应该在目录树中找到这样的类吗?

schema是您提供给它的json文件,用于定义表单

我查看了这个SO问题中的各种答案,以尝试解决该问题:

Django Admin-覆盖自定义窗口小部件表单字段

forms.py和ModelAdmin对象似乎足以实现此目的,但是不确定ModelAdmin对象应该在树中的哪个位置.

如何用Django实现?

  1. 从jsonschema呈现表单
  2. 在客户端验证用户针对jsonschema放入表单的内容
  3. 当用户单击"submit"作为json时,从POST请求表单中检索数据

我不确定这两个库是否是答案.

解决方案

PageAdmin类位于对应的model.py旁边的名为admin.py的文件中.这是有关发现管理员的Django 1.11文档文件.

您还必须为您的模型注册管理员.您使用django.contrib.admin.register 装饰器.同一页面上也包含此装饰器的文档.

用法类似

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)}
    }

此代码段将PageAdmin类注册为Page模型的ModelAdmin.

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:

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

解决方案

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.

这篇关于使用django-jsonschema-form或django-schemulator将jsonschema渲染为表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆