项目作者: collabo-br

项目描述 :
Django widgets to render jsonschema as admin forms
高级语言: Python
项目地址: git://github.com/collabo-br/django-jsonschema-form.git
创建时间: 2017-05-08T14:23:15Z
项目社区:https://github.com/collabo-br/django-jsonschema-form

开源协议:MIT License

下载


django-jsonschema-form

This package renders a jsonschema as part of one django admin form.

Instalation

Just run pip install django-jsonschema-form and then add an entry on your django project’s settings.

  1. INSTALLED_APPS = [
  2. ...,
  3. jsonschemaform,
  4. ]

How it works

The core component is basicaly a django widget that receives a jsonschema and renders a form fragment.
It uses the JSON Editor js library to actually render this fragment.

In practice you only have to override the admin widget like the snipet bellow.

  1. # Imporing Widget
  2. from jsonschemaform.admin.widgets.jsonschema_widget import JSONSchemaWidget
  3. # Here it is used postgres JSONField field implementation. Other implementation can be used depending on your DB
  4. from django.contrib.postgres.fields import JSONField
  5. # JSONSchema definition
  6. schema = {
  7. "title": "Config Schema",
  8. "description": "My configutation schema",
  9. "type": "object",
  10. "properties": {
  11. "columns": {
  12. "description": "List of columns size",
  13. "type": "array"
  14. },
  15. "class": {
  16. "description": "A reference css class",
  17. "type": "string"
  18. },
  19. "container": {
  20. "default": "container",
  21. "description": "Default page container",
  22. "type": "string"
  23. }
  24. },
  25. "required": [
  26. "columns",
  27. ],
  28. }
  29. # Overriding widgets for all instances of JSONField on PageAdmin form
  30. class PageAdmin(admin.ModelAdmin):
  31. formfield_overrides = {
  32. JSONField: {'widget': JSONSchemaWidget(schema)}
  33. }

This form will looks like

rendered Jsonschema

Tweaking the editor

It is possible to configure the editor through django settings using the key JSONSCHEMAFORM.

  1. JSONSCHEMAFORM = {
  2. 'css': {
  3. 'all': (
  4. 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
  5. )
  6. },
  7. 'options': {
  8. 'theme': 'bootstrap3',
  9. 'iconlib': 'bootstrap3',
  10. 'no_additional_properties': True,
  11. 'disable_collapse': True,
  12. }
  13. }

The settings above is also the default configuration.

But you can override or add any options described on JSON Editor options.