[Answered ]-Update user profile and user in one request

2👍

Disclaimer

I was finally able to figure it out and I am only answering for future reference. I am not sure if this is best practice or the easiest way to accomplish the task. However, this is what I was able to come up with.

I changed the view to be simpler instead of that nasty looking filter query I found I could do a get object and just say user=self.request.user and that would get what I needed.

View

class UserProfileUpdateView(generics.UpdateAPIView):
    authentication_classes = (authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = UserProfileSerializer

    def get_object(self):
        return UserProfile.objects.get(user=self.request.user)

Then in the serializer I figured out how to update nested objects which is exactly what needed to be done. Now I realize that currently I am only really updating the built in django object. But soon I will be adding more fields to the user profile and will need to update those and if there are changes to the user object I wanted to update them in the same request.

In the update function you can access the request data with self.data and in there I was able to access the dict containing the user information. With that I was able to query for the user object, then using the UserSerializer I was able to validate the data and call update passing the user I queried for and the validated data from the serializer.

Serializer

class UserProfileSerializer(ModelSerializer):
    user = UserSerializer(required=True, many=False)
    games = UserGameProfileSerializer(required=False, many=True)

    class Meta:
        model = UserProfile
        fields = ('premium', 'user', 'games')
        read_only_fields = ('premium', )

    def update(self, instance, validated_data):
        user_data = validated_data.pop('user')
        game_data = validated_data.pop('games')
        username = self.data['user']['username']
        user = User.objects.get(username=username)
        print user
        user_serializer = UserSerializer(data=user_data)
        if user_serializer.is_valid():
            user_serializer.update(user, user_data)
        instance.save()
        return instance

Leave a comment