项目作者: m19t12

项目描述 :
Django JSON Schema Field
高级语言: Python
项目地址: git://github.com/m19t12/django-jsonb-schema.git
创建时间: 2017-12-11T14:11:27Z
项目社区:https://github.com/m19t12/django-jsonb-schema

开源协议:GNU General Public License v3.0

下载


Django JSON Schema

Build Status
Coverage Status

Django JSON Schema Field.

Table of content

Introduction

Django JSON Schema is a library for displaying and validating
postgresql jsonb data.

Installing

  1. pip install django_jsonb_schema

Usage

First you create a schema class.

Schema class is a django abstract model class (abstract = True).

You can import SchemaMeta class or create an abstract class.

If you want to validate and display nested data you can do this with the SchemaForeignKey.

For example to validating this dict data.

  1. {
  2. 'name': 'parent',
  3. 'age': 50,
  4. 'son': {
  5. 'name': 'son',
  6. 'age': '15',
  7. 'school': {
  8. 'name': 'school',
  9. 'address': 'school address'
  10. }
  11. }
  12. }

We create this three schema classes.

We can use any django model or third party field.

  1. #schemas.py
  2. from json_schema.models import SchemaForeignKey, SchemaMeta
  3. class SchoolSchema(models.Model):
  4. name = models.CharField(max_length=128)
  5. address = models.CharField(max_length=128)
  6. Meta = SchemaMeta()
  7. class SonSchema(models.Model):
  8. name = models.CharField(max_length=128)
  9. age = models.IntegerField()
  10. school = SchemaForeignKey(SchoolSchema, on_delete=models.CASCADE)
  11. Meta = SchemaMeta()
  12. class ParentSchema(models.Model):
  13. name = models.CharField(max_length=128, default="test")
  14. age = models.IntegerField()
  15. son = SchemaForeignKey(SonSchema, on_delete=models.CASCADE)
  16. Meta = SchemaMeta()

After we create our model.

  1. #models.py
  2. from django.db import models
  3. from json_schema.fields import JSONSchemaField
  4. from .schemas import ParentSchema
  5. class JSONSchemaModel(models.Model):
  6. simple_text = models.CharField(blank=True, null=True, max_length=256)
  7. simple_int = models.IntegerField(blank=True, null=True)
  8. parent = JSONSchemaField(schema=ParentSchema, blank=True)

TODO

Support array elements.