Django Rest Framework has metadata
classes. But you can augment it with another library called drf-schema-adapter.
- pip install drf-schema-adapter which should be 0.9.43
- go to settings.py and add this
'DEFAULT_METADATA_CLASS': 'drf_auto_endpoint.metadata.AutoMetadata', to your REST_FRAMEWORK settings
- add in this brand new setting
DRF_AUTO_METADATA_ADAPTER = 'drf_auto_endpoint.adapters.ReactJsonSchemaAdapter' in the same file
It should look like this:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
],
'DEFAULT_METADATA_CLASS': 'drf_auto_endpoint.metadata.AutoMetadata',
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
'PAGE_SIZE': 20,
'SEARCH_PARAM': 'q'
}
DRF_AUTO_METADATA_ADAPTER = 'drf_auto_endpoint.adapters.ReactJsonSchemaAdapter'
Choosing ReactJsonSchemaAdapter is a purely personal preference. You can also stick with the SimpleMetadata from DRF itself.
use something like postman to test on the same url but using OPTIONS as the method
You should get back something like this:
{
"data": {
"name": "Shape Detail",
"description": "Retrieve a shape by its id.",
"renders": [
"application/json"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"actions": {
"PUT": {
"id": {
"type": "integer",
"required": false,
"read_only": true,
"label": "ID"
},
"name": {
"type": "string",
"required": true,
"read_only": false,
"label": "Shape Name",
"max_length": 100
},
"name_en": {
"type": "string",
"required": false,
"read_only": false,
"label": "Shape Name [en]",
"max_length": 100
},
"name_zh_hans": {
"type": "string",
"required": false,
"read_only": false,
"label": "Shape Name [zh-hans]",
"max_length": 100
},
"serial_number": {
"type": "string",
"required": false,
"read_only": false,
"label": "Shape Number",
"max_length": 100
},
"shape_variant_number": {
"type": "string",
"required": false,
"read_only": false,
"label": "Shape Variant Number",
"max_length": 100
},
"collection": {
"type": "string",
"required": false,
"read_only": false,
"label": "Collection",
"max_length": 255
},
"qr_code": {
"type": "string",
"required": false,
"read_only": false,
"label": "QR Code",
"max_length": 255
},
"extra_number": {
"type": "string",
"required": false,
"read_only": false,
"label": "Extra Number",
"max_length": 255
},
"art_numbers": {
"type": "field",
"required": false,
"read_only": false,
"label": "Art numbers"
},
"remark": {
"type": "string",
"required": false,
"read_only": false,
"label": "Remark",
"max_length": 400
},
"has_associated_product_variant": {
"type": "field",
"required": false,
"read_only": true,
"label": "Has associated product variant"
},
"shape_benchmark": {
"type": "nested object",
"required": false,
"read_only": false,
"label": "Shape benchmark",
"children": {
"id": {
"type": "integer",
"required": false,
"read_only": true,
"label": "ID"
},
"up_spin_speed": {
"type": "string",
"required": false,
"read_only": false,
"label": "Up Spin Speed",
"max_length": 15
},
Will add more details to this should i learn more