Django尝试保持映像时出现框架错误 [英] Django rest framework errors in trying to persist an image
本文介绍了Django尝试保持映像时出现框架错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的UserProfile对象,
This is my UserProfile object,
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE, )
badge = models.ImageField(upload_to='media/badges/', null=True)
reputation = models.IntegerField(default=0)
status = models.CharField(max_length=255, null=True, blank=True)
thumbnail = models.ImageField(upload_to='media/user/', blank=True, null=True)
这是序列化程序,
class UserProfileSerializer(serializers.ModelSerializer):
thumbnail = serializers.ImageField(max_length=None, use_url=True, read_only=True)
class Meta:
model = models.UserProfile
fields = ('badge', 'reputation', 'status', 'thumbnail',)
这是尝试保存图像的api,
This the api that tries to save the image,
class CreateUpdateUserThumbnail(views.APIView):
def post(self, request, **kwargs):
user = User.objects.get(id=kwargs.get('user_id'))
user.profile.thumbnail = UserProfileSerializer(instance=user.profile, data=request.data)
if user.profile.thumbnail.is_valid():
user.profile.thumbnail.save()
return Response(user.profile.thumbnail.data, status=status.HTTP_201_CREATED)
else:
return Response(user.profile.thumbnail.errors, status=status.HTTP_400_BAD_REQUEST)
当我尝试将图像上传到此端点时,这是我得到的错误,
When I try to upload an image to this endpoint this is the error that I get,
AttributeError: 'UserProfileSerializer' object has no attribute '_committed'
我在这里做什么错了?
推荐答案
您是否将MEDIA ROOT放在URL之后?
Have you put MEDIA ROOT to after urls ?
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这篇关于Django尝试保持映像时出现框架错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文