Package: djangorestframework-jsonschema
djangorestframework-jsonschemaOverviewThis package provides a management command that builds a
Django-REST-Framework solution from a JSONSchema specification.RequirementsPython (3.8+)Django (3.0+)Django-Rest-Framework (3.11)JSONSchema (3.2)Jinja2 (2.11)InstallationInstall usingpip...$pipinstalldjangorestframework-jsonschemaThis app needs to included it in theINSTALLED_APPSof your project, it should
come afterrest_frameworkanddjango_filters, both of which are required,
but before anything specific to your project.INSTALLED_APPS=["django.contrib.admin",..."rest_framework","django_filters","jsonschema2dj",...]ExampleSave the following asschema.jsonin a new DRF appexample_app{"definitions":{"identifiers":{"properties":{"ISBN":{"type":"string","pattern":"[0-9]*[-| ][0-9]*[-| ][0-9]*[-| ][0-9]*[-| ][0-9]*"},"Dewey Decimal Classification":{"type":"string","pattern":"\\d{3}|\\d{3}\\.\\d+|[12456]--\\d+|3[ABC]?--\\d+"}}},"Book":{"properties":{"title":{"type":"string"},"other":{"$ref":"#/definitions/identifiers"},"genre":{"enum":["celebrity-nonsense","military-tat","other"]},"author":{"$ref":"#/definitions/Author"}}}},"properties":{"Book":{"$ref":"#/definitions/Book"},"Author":{"properties":{"name":{"type":"string"},"date_of_birth":{"type":"string","format":"date"}}}}}now run$pythonmanage.pyjsonschema2djexample_appand the following are produced:models.pyviews.pyserializers.pyurls.pyfilters.pyadmin.pyThese files are built using jinja templates using sensible default choices
where possible. It is expected that the user will modify these files to suit their
needs.eg.models.pywould be:classBook(models.Model):title=models.CharField(null=True,max_length=255)other=JSONField(validators=[JSONSchemaValidator({'$ref':'#/definitions/identifiers'},DEFINITIONS)])genre=models.CharField(null=True,max_length=25,choices=[('celebrity_nonsense','celebrity nonsense'),('military-tat','military-tat'),('other','other')])author=models.ForeignKey("Author",null=True,on_delete=models.CASCADE)classAuthor(models.Model):name=models.CharField(null=True,max_length=255)date_of_birth=models.DateField(null=True)This is intended to be:accessible to anyone with knowledge of JSONSchemaextensible by anyone with a rudimentary understanding of DjangoRulesModels are objects at the root level of thepropertiesof theschema.json.A model's fields are its root level properties.The definitions maybe used freely without constraint.There are three types of field:simple fieldsIf a field has ajsonschema:typethat is anything other than anobjectoritemsor this can be inferred then this field is mapped to a djngo field
approximately as:"string" -> CharField"integer" -> IntegerField"number" -> DecimalField"boolean" -> BooleanFieldIf the field's name isidthen this wil be the primary key.reference-fieldsIf a field has ajsonschema:typethat is anobjectoritemsand this references an object (model) with a schema also defined in the top level
properties then this is modeled as relationship to that object (model) like:"$ref": "Model-X"-> one-to-one or one-to-many"items": {"$ref": "Model-X"}-> many-to-one or many-to-manyNote that the exact cardinality can only be determined by comparing both sides of
the relationship. If only one side is specified the it is assumed that it is
one-to-many or many-to-many respectively.json-fieldsLastly if a field has ajsonschema:typethat is anobjectoritemsand this references an object (model) with a schema that is not
defined in the top level properties then this is modeled as a jsons object
in its own right, i.e.:"object" -> JSONFieldIts schema will be enfoced by the serializer.Testing$./runtests.py