我是django rest框架的新手。我正在使用DRF和DRF yasg Swagger。任何机构都可以帮助从请求机构上传图像吗?我无法从请求主体上传图像。我的当前视图.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()
有人能解释这个问题吗?
在视图中将FormParser
类设置为
from rest_framework.parsers import FormParser
from rest_framework.decorators import parser_classes
@swagger_auto_schema(method='post', request_body=MySerializer)
@api_view(['POST'])
@parser_classes((FormParser,))
def image(request):
profile_img = request.FILES.get('profile_img', '')
cover_img = request.FILES.get('cover_img', '')
使用MultiPartParse
就像使用图像一样。
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', '')