简体   繁体   中英

drf-yasg: Image field not displaying in swagger ui

I am new to django-rest-framework. I am using DRF and drf-yasg Swagger. Can any body help to upload image from request body? I am not able to upload image from request body. My current view.py:

@swagger_auto_schema(method='post', request_body=MySerializer)
@api_view(['POST'])
def image(request):
    profile_img = request.FILES.get('profile_img', '')
    cover_img = request.FILES.get('cover_img', '')
    ...
    pass

my_serializer.py:

class MySerializer(serializers.Serializer):
    profile_img = serializers.ImageField()
    profile_img = serializers.ImageField()

Can any one explain the problem?

Set FormParser class in your view function as,




@swagger_auto_schema(method='post', request_body=MySerializer)
@api_view(['POST'])

def image(request):
    profile_img = request.FILES.get('profile_img', '')
    cover_img = request.FILES.get('cover_img', '')

Use MultiPartParse as you are using images.

from rest_framework.parsers import MultiPartParser
from rest_framework.decorators import parser_classes

@parser_classes((MultiPartParser,))
def image(request):
    profile_img = request.FILES.get('profile_img', '')
    cover_img = request.FILES.get('cover_img', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM