Giter Club home page Giter Club logo

django-jsonb-schema's Introduction

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

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.

{
    'name': 'parent',
    'age': 50,
    'son': {
        'name': 'son',
        'age': '15',
        'school': {
            'name': 'school',
            'address': 'school address'
        }
    }
}

We create this three schema classes.

We can use any django model or third party field.

#schemas.py
from json_schema.models import SchemaForeignKey, SchemaMeta


class SchoolSchema(models.Model):
    name = models.CharField(max_length=128)
    address = models.CharField(max_length=128)
    Meta = SchemaMeta()


class SonSchema(models.Model):
    name = models.CharField(max_length=128)
    age = models.IntegerField()
    school = SchemaForeignKey(SchoolSchema, on_delete=models.CASCADE)
    Meta = SchemaMeta()


class ParentSchema(models.Model):
    name = models.CharField(max_length=128, default="test")
    age = models.IntegerField()
    son = SchemaForeignKey(SonSchema, on_delete=models.CASCADE)
    Meta = SchemaMeta()

After we create our model.

#models.py
from django.db import models

from json_schema.fields import JSONSchemaField
from .schemas import ParentSchema

class JSONSchemaModel(models.Model):
    simple_text = models.CharField(blank=True, null=True, max_length=256)
    simple_int = models.IntegerField(blank=True, null=True)
    parent = JSONSchemaField(schema=ParentSchema, blank=True)

TODO

Support array elements.

django-jsonb-schema's People

Contributors

dependabot[bot] avatar m19t12 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.