Skip to content

Instantly share code, notes, and snippets.

@parsaM110
Created April 23, 2025 14:03
Show Gist options
  • Select an option

  • Save parsaM110/e73dc47ee13c46b898c6a933318238cf to your computer and use it in GitHub Desktop.

Select an option

Save parsaM110/e73dc47ee13c46b898c6a933318238cf to your computer and use it in GitHub Desktop.
Django-yasg
pip install drf-yasg

use 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
    pass

As 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)
@parsaM110
Copy link
Author

https://www.youtube.com/watch?v=XOB-aHKu6e4
best video for start and install and get started

pip install drf-yasg

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment