0

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?

1
  • your serializer field is read only and it will not save any way, why you set it read only? Commented May 27, 2018 at 17:31

1 Answer 1

0

Have you put MEDIA ROOT to after urls ?

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.