DRF JSON Schema Serializer
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
-
ImportError: cannot import name 'JSONSchemaField' from 'drf_jsonschema_serializer'
cause Attempting to import or use the deprecated `JSONSchemaField` which was removed in version 3.0.0.fixReplace `JSONSchemaField` with `JSONSchemaDictField` for dictionaries or `JSONSchemaListField` for lists. Update your import statement to `from drf_jsonschema_serializer.fields import JSONSchemaDictField` (or `JSONSchemaListField`). -
RuntimeError: 'drf_jsonschema_serializer' requires Python 3.10 or later.
cause Your Python environment does not meet the minimum requirement of Python 3.10 for drf-jsonschema-serializer v3.x.fixUpgrade your Python installation to version 3.10 or newer. If you need to use an older Python version, you must use an older compatible version of drf-jsonschema-serializer (e.g., v2.x for Python 3.8/3.9). -
django.core.exceptions.ImproperlyConfigured: 'drf_jsonschema_serializer' requires Django 3.2 or later.
cause Your Django version is older than the minimum requirement of 3.2 for drf-jsonschema-serializer v3.x.fixUpgrade your Django installation to version 3.2 or newer. Similarly for Django REST Framework (>=3.12).
Warnings
- breaking Version 3.0.0 drops support for Python < 3.10, Django < 3.2, and Django REST Framework < 3.12. Ensure your environment meets these minimum requirements before upgrading.
- breaking The `JSONSchemaField` class was removed in version 3.0.0. If you were using it for dict or list fields within your DRF serializers, you must migrate to `JSONSchemaDictField` or `JSONSchemaListField` respectively.
- deprecated The `JSONSchemaView` for serving schema endpoints was removed in v2.0.0. While this predates v3.0.0, users migrating from very old versions might encounter issues. The library now focuses purely on schema generation.
Install
-
pip install drf-jsonschema-serializer
Imports
- JSONSchemaSerializer
from drf_jsonschema_serializer import JSONSchemaSerializer
- JSONSchemaDictField
from drf_jsonschema_serializer import JSONSchemaField
from drf_jsonschema_serializer.fields import JSONSchemaDictField
- JSONSchemaListField
from drf_jsonschema_serializer import JSONSchemaField
from drf_jsonschema_serializer.fields import JSONSchemaListField
Quickstart
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']
# }