17π
β
You should be able to use @renderer_classes
decorator:
from rest_framework_swagger import renderers
from rest_framework.decorators import api_view, renderer_classes
@api_view(['GET', 'POST'])
@renderer_classes([renderers.OpenAPIRenderer, renderers.SwaggerUIRenderer])
def app_info(request):
...
return response
Also, it should be worth mentioning, that if you donβt want to use this decorator on every view you can specify DEFAULT_RENDERER_CLASSES
in settings
EDIT: It seems itβs in the docs after all. Check the very bottom of this page: https://django-rest-swagger.readthedocs.io/en/latest/schema/
π€Igonato
2π
i am not fammiliar with swagger,but you may try to use the decorator in this way:
class TestView(View):
@api_view(['GET', 'POST'])
def get(self, request):
....
or
from django.utils.decorators import method_decorator
class TestView(View):
@method_decorator(api_view(['GET', 'POST'])
def get(self, request):
....
βββββββββββββββββββββββββ-
sorry, maybe i have misunderstood your question. according to the document, if you want to enable swagger in class based view. there is example:
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework.views import APIView
from rest_framework_swagger import renderers
class SwaggerSchemaView(APIView):
permission_classes = [AllowAny]
renderer_classes = [
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]
def get(self, request):
generator = SchemaGenerator()
schema = generator.get_schema(request=request)
return Response(schema)
restframework will use these two renderer_classes to render Json and UI.
π€chujie.ao
2π
Add the following in your views.py
Imports
from rest_framework.schemas import AutoSchema
from rest_framework.compat import coreapi
#creating custom class
class CustomSampleSchema(AutoSchema):
def __init__(self):
super(CustomSampleSchema, self).__init__()
def get_manual_fields(self, path, method):
extra_fields = [
coreapi.Field('field1', required=True, location='form', description='', type='', example=''),
coreapi.Field('field2', required=False, location='form', description='', type='', example=''),
coreapi.Field('field3', required=False, location='form', description='', type='', example='')
]
manual_fields = super().get_manual_fields(path, method)
return manual_fields + extra_fields
This is the function youβre writing swagger doc for.
@api_view(['post'])
@schema(CustomSampleSchema())
@csrf_exempt
def func_name(request, param):
"""
Your function definition below
"""
Sample json input
{"name": "['name1', ]",
"places": "['place1', 'place2']",
"key":"12345"}
π€Nithin
- Check for request.GET variable in the template
- Django models across multiple projects/microservices. How to?
- Django template tag: How to send next_page in {url auth_logout}?
- Django nginx Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
- Django cms β invalid block tag endblock
Source:stackexchange.com