gpt4 book ai didi

python - Django Rest Framework 上传文件到方法

转载 作者:太空狗 更新时间:2023-10-29 21:00:54 25 4
gpt4 key购买 nike

所以我一直在尝试将文件上传到使用 DRF 的方法,但到目前为止没有成功。

我能够使用 (FormParser, MultiPartParser,) 上传到 ModelViewSet 没有任何问题,但我真的需要在这样的地方使用它 http://localhost:8000/api/v1/women/{pk}/upload_avatar/ 我想先通过 id 过滤女人并上传到她的头像(这是多媒体模型的外键) .我尝试使用嵌套资源库,但没有成功。

到目前为止,我的模型 View 集中有:

class WomenNativePassportViewSet(viewsets.ModelViewSet):
queryset = Women.objects.all()
serializer_class = WomenNativePassportSerializer
authentication_classes = (NoAuthentication,)
permission_classes = (AllowAny,)
parser_classes = (FormParser, MultiPartParser,)

@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, *args, **kwargs):
if 'POST' in request._method or 'PATCH' in request._method:
# Write code to save the file??
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)

我的模型:

class Women(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
avatar = models.ForeignKey(
'core.Multimedia', blank=True, null=True,
related_name='user_profiles_avatares'
)

class Multimedia(models.Model):
file = models.FileField(upload_to=upload_to, null=True, blank=True)
thumbnail = models.FileField(upload_to=upload_to, null=True, blank=True)

基本上我想知道这是否是我正在采取的正确路径,如果是,我如何才能在模型中正确保存上传的文件?

最佳答案

这是我为克服这个问题所做的一些代码。尽管 Kevin Brown 的回答可能有效,但我发现我的代码有点“简单”:

    @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)


# serializer

class AvatarSerializer(serializers.Serializer):
thumbnail = serializers.ImageField()

关于python - Django Rest Framework 上传文件到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26673572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com