这是我为克服此问题所做的一些代码。尽管凯文·布朗的回答可能行得通,但我发现我的代码有点“轻松”的方法:
@detail_route( methods=['post', 'put', 'patch', 'get'], permission_classes=[AllowAny], authentication_classes=[NoAuthentication], serializer_class=MultimediaSerializer, parser_classes=(FormParser, MultiPartParser,) ) def upload_avatar(self, request, pk=None): # Because we are using nested resources this was the only way i found to # upload a file. Maybe there is a better way if request.method in ['PATCH', 'POST']: avatar = request.FILES.get('avatar') if not avatar: return Response(status=404) try: woman = WomenNativePassport.objects.get(pk=pk) except WomenNativePassport.DoesNotExist: return Response(status=404) else: request.FILES['thumbnail'] = request.FILES['avatar'] serializer = AvatarSerializer( data=request.DATA, files=request.FILES ) if serializer.is_valid(): woman.avatar.thumbnail.save(str(avatar), File(avatar)) return Response(status=204) else: return Response(status=404) else: multimedia = Multimedia.objects.filter(user_profiles_avatares__pk=pk) page = self.paginate_queryset(multimedia) serializer = self.get_pagination_serializer(page) return Response(serializer.data)# serializerclass AvatarSerializer(serializers.Serializer): thumbnail = serializers.ImageField()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)