KeyError:"尝试在序列化程序'登录序列化程序'上获取字段'password'的值时出现KeyError。



我有一个类似的登录视图,但每当我访问API时,我都会得到一个KeyError。我似乎无法理解相关的答案。

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)

序列化程序如下所示:

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(),
}

当我访问这个端点时,我得到这个错误:

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: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

如何修复?

这就是我让它工作的方式。我让密码在LoginSerializer只写

之前:

password = serializers.CharField(min_length=6)

之后:

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

相关内容

最新更新