1

This is my UserProfile model,

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)

And this is the Serializer class,

class UserProfileSerializer(serializers.ModelSerializer):
    thumbnail = serializers.ImageField(max_length=None, use_url=True)

    class Meta:
        model = models.UserProfile
        fields = ('badge', 'reputation', 'status', 'thumbnail',)

My API to update an image looks like this,

class CreateUpdateUserThumbnail(views.APIView):

    def post(self, request, **kwargs):
        try:
            user = User.objects.get(id=kwargs.get('user_id'))
        except User.DoesNotExist:
            return Response({"Error": "User does not exist"}, status=status.HTTP_404_NOT_FOUND)
        request.data['user_id'] = user.id
        serializer = UserProfileSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

This is the error that I get when I try to upload an image using POSTMAN.

return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: bouncer_userprofile.user_id

How do I associate a user_id with the serializer data?

2
  • what about the answers is one help you? Commented May 27, 2018 at 17:15
  • @BearBrown I'm still having some issues, could you please take a look at this question. stackoverflow.com/questions/50554265/… Commented May 27, 2018 at 17:18

2 Answers 2

2

the request.data is immutable so you should to create the copy first:

data = request.data.copy()
data['user_id'] = user.id
serializer = UserProfileSerializer(data=data)
Sign up to request clarification or add additional context in comments.

Comments

1

You need to pass user_profile instance on UserProfileSerializer instantiation

serializer = UserProfileSerializer(instance=user.profile, data=request.data)

Your view will look like this

class CreateUpdateUserThumbnail(views.APIView):

    def post(self, request, **kwargs):
        try:
            user = User.objects.get(id=kwargs.get('user_id'))
        except User.DoesNotExist:
            return Response({"Error": "User does not exist"}, status=status.HTTP_404_NOT_FOUND)
        serializer = UserProfileSerializer(instance=user.profile, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

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.