django json schema将django表单转换为json模式兼容表示
django-jsonschema的Python项目详细描述
简介
django json schema将django表单转换为json模式兼容表示形式
要求
- python 2.6或更高版本
- django 1.4或更高版本
用法
将表单转换为json模式:
from djangojsonschema.jsonschema import DjangoFormToJSONSchema schema_repr = DjangoFormToJSONSchema().convert_form(MyForm)
将json模式嵌入为表单字段:
from djangojsonschema.forms import JSONSchemaField #where schema is a python dictionay like schema_repr in the first exmaple class MyForm(forms.Form): subfield = JSONSchemaField(schema=schema) form = MyForm(data={'subfield':'<json encoded value>'}) form.validate() #will validate the subfield entry against schema form['subfield'].as_widget() #will render a textarea widget with a data-schemajson attribute