KeyError: "Got KeyError when attempting to get a value for field `password` on serializer `LoginSerializer`

I have a login view that looks like this but I keep getting a KeyError whenever I access the API. I cannot seem to fix with the related answers.

class LoginAPIView(GenericAPIView):
    serializer_class = LoginSerializer

    def post(self, request):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

The serializer looks like this:

class LoginSerializer(serializers.ModelSerializer):
    email = serializers.EmailField()
    password = serializers.CharField(min_length=6)
    username = serializers.CharField(read_only=True)
    tokens = serializers.CharField(read_only=True)

    class Meta:
        model = User
        fields = ["email", "password", "username", "tokens"]

    def validate(self, attrs):
        email = attrs.get("email", "")
        password = attrs.get("password", "")

        user = auth.authenticate(email=email, password=password)

        if not user:
            raise AuthenticationFailed("Invalid credentials")
        if not user.is_active:
            raise AuthenticationFailed("Account is not active, please contact admin")

        return {
            "email": user.email,
            "username": user.username,
            "tokens": user.tokens(),
        }

when I access this endpoint, I get this error:

KeyError: "Got KeyError when attempting to get a value for field `password` on serializer `LoginSerializer`.\nThe serializer field might be named incorrectly and not match any attribute or key on the `dict` instance.\nOriginal exception text was: 'password'."

full error below.

[08/Oct/2022 23:29:57] "POST /auth/register/ HTTP/1.1" 201 54
Internal Server Error: /auth/login/
Traceback (most recent call last):
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/fields.py", line 446, in get_attribute
    return get_attribute(instance, self.source_attrs)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/fields.py", line 94, in get_attribute
    instance = instance[attr]
KeyError: 'password'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/xorlali/Projects/refive-backend/src/authentication/views.py", line 93, in post
    return Response(serializer.data, status=status.HTTP_200_OK)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/serializers.py", line 555, in data
    ret = super().data
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/serializers.py", line 255, in data
    self._data = self.to_representation(self.validated_data)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/serializers.py", line 509, in to_representation
    attribute = field.get_attribute(instance)
  File "/home/xorlali/.cache/pypoetry/virtualenvs/refive-backend-0hCIKWkR-py3.9/lib/python3.9/site-packages/rest_framework/fields.py", line 479, in get_attribute
    raise type(exc)(msg)
KeyError: "Got KeyError when attempting to get a value for field `password` on serializer `LoginSerializer`.\nThe serializer field might be named incorrectly and not match any attribute or key on the `dict` instance.\nOriginal exception text was: 'password'."
[08/Oct/2022 23:30:25] "POST /auth/login/ HTTP/1.1" 500 21187

How do I get it fixed?

This is how I got it to work. I made the password in the LoginSerializer write only

before:

password = serializers.CharField(min_length=6)

after:

password = serializers.CharField(min_length=6, write_only=True)
Back to Top