如何序列化多个InMemoryUploadedFile
使用serializers.ListField()
?
代码片段
#views.py
@api_view(['POST', 'GET'])
def create_post(request):
if request.method == 'POST':
altered_request_data = request.data.copy()
in_memory_upload_files_list = [value for value in request.FILES.values()]
altered_request_data['files'] = in_memory_upload_files_list
serializer = PostSerializer(data=altered_request_data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(data=serializer.data, status=201)
else:
qs = Post.objects.all()
serializer = PostSerializer(qs, many=True)
return Response(serializer.data)
#serilizers.py
class PostSerializer(serializers.ModelSerializer):
files = serializers.ListField(child=serializers.FileField(), write_only=True)
class Meta:
fields = '__all__'
model = Post
当前响应
{
"files": {
"0": [
"The submitted data was not a file. Check the encoding type on the form."
]
}
}