pip install drf-yasguse it like this in views.py
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
@swagger_auto_schema(
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'user': openapi.Schema(
type=openapi.TYPE_OBJECT,
description='user',
properties={
'first_name': openapi.Schema(type=openapi.TYPE_STRING, description='string', example="john"),
'last_name': openapi.Schema(type=openapi.TYPE_STRING, description='string', example="Doe"),
'phone_number': openapi.Schema(type=openapi.TYPE_STRING, description='string', example=' 01379644081'),
'email': openapi.Schema(type=openapi.TYPE_STRING, description='string', example="johnDoe@gmail.com"),
}
)
}
),
operation_description="Your operation description here"
)
def your_view_function(request):
# your view logic
passAs a reusable schema component :
response_schema_dict = {
"200": openapi.Response(
description="OK",
examples={
"application/json": {
"pan": "6221064828589925",
"first_name": "john",
"last_name": "Doe",
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTY4ODc3OTEyMSwiaWF0IjoxNjg4NjkyNzIxLCJqdGkiOiI3YzViNTljYWY2OWY0NDE1YjA4Y2VlMTMzMTdlODIxNyIsInVzZXJfaWQiOjF9.GiZS-2Zu8rwptMGOEAMc91KEEe7WmHAgO0NwB9YREEI",
"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjg4Nzc5MTIxLCJpYXQiOjE2ODg2OTI3MjEsImp0aSI6ImQ2ZGQ2YjZkYjY4ODRjNzA5Yzk4OGZmNjU5NDMwMTdlIiwidXNlcl9pZCI6MX0._en2Cnk_V9nC63pQwiLSM74Fh4D5v6-YhCq0cyhNNDI"
}
}
),
"401": openapi.Response(
description="Error: Unauthorized",
examples={
"application/json": {
}
}
),
}
login_request_body = openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'phone_number': openapi.Schema(type=openapi.TYPE_STRING, description='The phone number',example="01379644081"),
'p2': openapi.Schema(type=openapi.TYPE_STRING, description='The Second Password',example="1111"),
})
@swagger_auto_schema(request_body=login_request_body,responses=response_schema_dict)
https://www.youtube.com/watch?v=XOB-aHKu6e4
best video for start and install and get started
sample responses in swagger π
drf-yasg How to show sample response with with api? - Stack Overflow
swagger custom json body for POST π
drf-yasg custom json body - Stack Overflow
auth ways (Basic , token (Bearer)):
Settings β drf-yasg 1.21.6 documentation
How to add prefix "token" on django-yasg? - Stack Overflow
some yuk over post request in swagger handled by serializer (seems unnecessary ) π:
Django Swagger won't allow me to use POST method (no parameters shown) - Stack Overflow
example value in swagger π
how to specify example value on drf-yasg swagger_auto_schema request_body? - Stack Overflow
use generic views with mixins (seems unnecessary) π:
Generic views - Django REST framework