Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The GIST includes code to add schema in API with DRF YASG library.
from rest_framework import serializers
from .models import *
class DataSerializer(serializers.ModelSerializer):
class Meta:
model = tbl_data_master
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="API V1",
default_version='v1',
description="This is V1 API's",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="demo@gmail.com"),
license=openapi.License(name="Test License"),
)
public=True,
permission_classes=(permissions.AllowAny)
)
urlpatterns = [
path('get_all_data/',api_get_data_view_by_id,name="all_data"),
]
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from rest_framework.decorators import api_view
from rest_framework.response import Response
"""
The swagger auto schema will generate textbox to input the data POST
we can also make it a GET method and apply same to provide variables
"""
@swagger_auto_schema(methods=['POST'], request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
required=['id'],
properties={
'id': openapi.Schema(type=openapi.TYPE_INTEGER)
},
), responses={200:DataSerializer})
@api_view(['POST',])
def api_get_data_view_by_id(request):
try:
data = tbl_data_master.objects.filter(id=request.data['id'],status='Active')
except tbl_data_master.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == "POST":
if not data:
return Response([])
serializer = DataSerializer(data,many=True)
return Response(serializer.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment