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)
This is the Serializer,
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',)
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'
What am I doing wrong here?
read onlyand it will not save any way, why you set itread only?