如何通过 Django Rest Framework 上传图片?
How do I upload an image via Django Rest Framework?
这是我的模型,包含 ImageField
:
class Profile(models.Model):
user = models.OneToOneField(User)
new_article_notifications_enabled = models.BooleanField(default=False)
new_comment_notifications_enabled = models.BooleanField(default=False)
new_comment_answer_notifications_enabled = models.BooleanField(default=False)
userpic = models.ImageField(upload_to='userpics/', blank=True, null=True)
city = models.PositiveSmallIntegerField(choices=CITY_CHOICES, default=CITY_CHOICES[0][0])
webpage = models.URLField(blank=True)
about_me = models.TextField(blank=True)
favorite_brands = models.TextField(blank=True)
序列化器:
class ProfileChangeSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = [
'new_article_notifications_enabled',
'new_comment_notifications_enabled',
'new_comment_answer_notifications_enabled',
'userpic',
'city',
'webpage',
'about_me',
'favorite_brands',
]
def update(self, instance, validated_data):
Profile.objects.filter(id=instance.pk).update(**validated_data)
return instance
查看:
class ProfileChangeAPIView(generics.RetrieveAPIView,
mixins.DestroyModelMixin,
mixins.UpdateModelMixin):
permission_classes = (
permissions.IsAuthenticated,
)
serializer_class = ProfileChangeSerializer
parser_classes = (MultiPartParser, FormParser,)
def get_object(self):
if not Profile.objects.filter(user__id=self.request.user.pk).exists():
return Profile.objects.create(user=self.request.user)
return self.request.user.profile
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
因此,当我尝试通过 Django Rest Framework 界面上传用户图片时 - 它会更新图像的超链接,但不会在 media
文件夹中创建图像文件。当我通过 django 管理界面上传图片时一切正常。我哪里错了?
我认为您不需要重写 Serializer update
方法。您可以简单地在视图中覆盖 put
,如下所示:
def put(self, request, *args, **kwargs):
serializer = ProfileChangeSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
顺便问一下,你是如何从前端发送图像数据的?我希望你像
一样使用 jquery FormData
这是我的模型,包含 ImageField
:
class Profile(models.Model):
user = models.OneToOneField(User)
new_article_notifications_enabled = models.BooleanField(default=False)
new_comment_notifications_enabled = models.BooleanField(default=False)
new_comment_answer_notifications_enabled = models.BooleanField(default=False)
userpic = models.ImageField(upload_to='userpics/', blank=True, null=True)
city = models.PositiveSmallIntegerField(choices=CITY_CHOICES, default=CITY_CHOICES[0][0])
webpage = models.URLField(blank=True)
about_me = models.TextField(blank=True)
favorite_brands = models.TextField(blank=True)
序列化器:
class ProfileChangeSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = [
'new_article_notifications_enabled',
'new_comment_notifications_enabled',
'new_comment_answer_notifications_enabled',
'userpic',
'city',
'webpage',
'about_me',
'favorite_brands',
]
def update(self, instance, validated_data):
Profile.objects.filter(id=instance.pk).update(**validated_data)
return instance
查看:
class ProfileChangeAPIView(generics.RetrieveAPIView,
mixins.DestroyModelMixin,
mixins.UpdateModelMixin):
permission_classes = (
permissions.IsAuthenticated,
)
serializer_class = ProfileChangeSerializer
parser_classes = (MultiPartParser, FormParser,)
def get_object(self):
if not Profile.objects.filter(user__id=self.request.user.pk).exists():
return Profile.objects.create(user=self.request.user)
return self.request.user.profile
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
因此,当我尝试通过 Django Rest Framework 界面上传用户图片时 - 它会更新图像的超链接,但不会在 media
文件夹中创建图像文件。当我通过 django 管理界面上传图片时一切正常。我哪里错了?
我认为您不需要重写 Serializer update
方法。您可以简单地在视图中覆盖 put
,如下所示:
def put(self, request, *args, **kwargs):
serializer = ProfileChangeSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
顺便问一下,你是如何从前端发送图像数据的?我希望你像