DRF JSON Schema Serializer

3.0.0 · active · verified Fri Apr 17

drf-jsonschema-serializer provides automatic JSON Schema generation for Django REST Framework serializers. It allows developers to define DRF serializers as usual and then easily convert them into JSON Schema Draft 2020-12, OpenAPI 3.0.x compatible schemas. The current version is 3.0.0, with releases typically tied to major Django/DRF version compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a standard Django REST Framework serializer and then use `drf-jsonschema-serializer` to automatically generate its corresponding JSON Schema. The generated schema can be used for API documentation, client-side validation, or other schema-driven processes.

from rest_framework import serializers
from drf_jsonschema_serializer import JSONSchemaSerializer

class MyInputSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=100, help_text="The name of the item")
    quantity = serializers.IntegerField(min_value=1, help_text="The quantity of the item")
    is_active = serializers.BooleanField(default=True)

# Instantiate the JSONSchemaSerializer with your DRF serializer
schema_generator = JSONSchemaSerializer(MyInputSerializer())

# Generate the JSON Schema
json_schema = schema_generator.data

print(json_schema)
# Example expected output (truncated):
# {
#   'type': 'object',
#   'properties': {
#     'name': {'type': 'string', 'maxLength': 100, 'description': 'The name of the item'},
#     'quantity': {'type': 'integer', 'minimum': 1, 'description': 'The quantity of the item'},
#     'is_active': {'type': 'boolean', 'default': True}
#   },
#   'required': ['name', 'quantity']
# }

view raw JSON →