Django Foreign Key not getting updated

Viewed 34

Hi Guys I'm new to Django and I'm struggling with a problem that should be easy to be solved. This is my UserSerializer:

class UserSerializer(serializers.ModelSerializer):
    role = RoleSerializer(many=False)

    class Meta:
        model = User
        fields = ['id', 'first_name', 'last_name', 'email', 'password', 'role', 'role_id']
        extra_kwargs = {'password': {'write_only': True},}

This is my GenericAPIView:

class UserGenericAPIView(generics.GenericAPIView, mixins.UpdateModelMixin):
    serializer_class = UserSerializer
    queryset = User.objects.all()

    def put(self, request, pk=None):
        return self.partial_update(request, pk)

The request that the frontend sends is this:

PUT http://localhost:8000/api/users/1/
Content-Type: application/json

{
   "first_name": "First Name",
   "last_name": "Last Name",
   "email": "email@example.com",
   "role_id": 2
}

All the fields change except for the role_id... Any idea what is the best way to update the role_id?

0 Answers
Related