djangorestframework-jsonschema
Overview
This package provides a management command that builds a Django-REST-Framework solution from a JSONSchema specification.
Requirements
- Python (3.8+)
- Django (3.0+)
- Django-Rest-Framework (3.11)
- JSONSchema (3.2)
- Jinja2 (2.11)
Installation
Install using pip...
$ pip install djangorestframework-jsonschema
This app needs to included it in the INSTALLED_APPS of your project, it should
come after rest_framework and django_filters, both of which are required,
but before anything specific to your project.
INSTALLED_APPS = [
"django.contrib.admin",
...
"rest_framework",
"django_filters",
"jsonschema2dj",
...
]
Example
Save the following as schema.json in a new DRF app example_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
$ python manage.py jsonschema2dj example_app
and the following are produced:
- models.py
- views.py
- serializers.py
- urls.py
- filters.py
- admin.py
These 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.py would be:
class Book(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)
class Author(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 JSONSchema
- extensible by anyone with a rudimentary understanding of Django
Rules
Models are objects at the root level of the properties of the schema.json.
A model's fields are its root level properties.
The definitions maybe used freely without constraint.
There are three types of field:
simple fields
If a field has a jsonschema:type that is anything other than an object or
items or this can be inferred then this field is mapped to a djngo field
approximately as:
- "string" -> CharField
- "integer" -> IntegerField
- "number" -> DecimalField
- "boolean" -> BooleanField
If the field's name is id then this wil be the primary key.
reference-fields
If a field has a jsonschema:type that is an object or
items and 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-many
Note 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-fields
Lastly if a field has a jsonschema:type that is an object or
items and 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" -> JSONField
Its schema will be enfoced by the serializer.
Testing
$ ./runtests.py
Metadata
Release files for djangorestframework-jsonschema 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| djangorestframework-jsonschema-0.1.1.tar.gz | 13.1 kB | Without provenance | Details |
Release files / djangorestframework-jsonschema-0.1.1.tar.gz
| Download URL | djangorestframework-jsonschema-0.1.1.tar.gz |
|---|---|
| Size | 13.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73b2209ef1c72ce03e8255223476762eb8463dd21046bd8a6fa4f4c641e5d923
|
|
BLAKE2b-256 checksum How to use checksums |
ff2f2c439578c0a81ae5f4ddd3f21f1c1812935733c8a344492910d5ed26fb2c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2
|
Security for djangorestframework-jsonschema 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.