I have this function-based view with django-rest-swagger decorated. However, I can't find place in UI that allow me to post the payload (request.body).
I saw a couple solutions about doing it with class-based view, but I was wondering if there is a way to do it with function-based view.
Thank you in advance!
@renderer_classes([JSONRender])
@api_view(['POST'])
def some_method(request):
body = json.loads(request.body)
return JsonResponse({'status': 'ok'})
I am gonna answer my question since the django-rest-swagger was deprecated in June 2019 and I just found out 2 feasible solutions.
First one will change the UI globally.
In
ping.views
(or any other location you wish) add following class.Add following settings in
settings.py
of your Django project.Second solution can be applied on a per-view basis. You may check here for official guide
Use @schema from
rest_framework.decorators.schema
to overwriteDEFAULT_SCHEMA_CLASS
.Basically, the idea is to overwrite
DEFAULT_SCHEMA_CLASS
. The wordschema
is the term that they used to refer to swagger UI for each view inrest_framework
.When you use
@api_view()
to decorate your function-based view, it will assign your function an attributeschema
with valueAPIView.schema
fromrest_framework.views.APIView
.rest_framework.views.APIView
will in further callDefaultSchema()
to load theDEFAULT_SCHEMA_CLASS
from yourREST_FRAMEWORK
configuration insettings.py
.Without other specifying,
DEFAULT_SCHEMA_CLASS
isrest_framework.schemas.openapi.AutoSchema
by this official announcement. You might want to change it torest_framework.schemas.coreapi.AutoSchema
since it is the one that compatible withdjango_rest_swagger
.Hope this tutorial helps people who use
django-rest-swagger (2.2.0)
with function-based views for their Django project.Please leave comments if there are anything I can help on this issue.