django 当代码未传递给User.objects.create时,User对象没有属性“code”

xqk2d5yq  于 2023-01-10  发布在  Go
关注(0)|答案(1)|浏览(131)

我正在使用Django Rest Framework来创建一个视图来验证一个OTP。目前,它工作得很好。models.py

class User(AbstractUser):
    phone_number = PhoneNumberField(blank=True)
    def __str__(self) -> str:
        return f"{self.username}"

views.py

class VerifyOTPCode(APIView):
    
    permission_classes=[AllowAny]
    
    def post(self, request):
        
        serializer = UserOTPCodeSerializer(data=request.data)
        
        if serializer.is_valid():
            user = serializer.save()
            return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors)

serializers.py

class UserOTPCodeSerializer(serializers.ModelSerializer):
    
    code = serializers.IntegerField()
    class Meta:
        model = User
        fields = ("first_name", "last_name", "email", "phone_number", "code")
    
    def validate(self, data):
        
        code, phone_number = str(data['code']), data['phone_number']
        
        if is_six_digits(code) and is_otp_approved(code, phone_number):
            return data
        
        raise serializers.ValidationError('Code was not approved')

    def create(self, validated_data):
        del validated_data['code']
        return User.objects.create(**validated_data)

但是,我想对视图使用泛型,所以我尝试将其重构为以下内容
views.py

class VerifyOTPCode(generics.CreateAPIView):
    
    permission_classes= [AllowAny]
    
    serializer_class= UserOTPCodeSerializer

现在,当我尝试命中端点时,在create处得到一个错误,即User对象没有属性code,即使createvalidated_data中删除了code,另一件事是,即使有这个错误,仍然会创建一个User对象。为什么会发生这个错误,我该如何修复它?

Internal Server Error: /user/register/verify-otp/
Traceback (most recent call last):
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\fields.py", line 457, in get_attribute
    return get_attribute(instance, self.source_attrs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\fields.py", line 97, in get_attribute
    instance = getattr(instance, attr)
AttributeError: 'User' object has no attribute 'code'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", 
line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", 
line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
480, in raise_uncaught_exception
    raise exc
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\generics.py", line 190, in post
    return self.create(request, *args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\mixins.py", line 20, in create
    headers = self.get_success_headers(serializer.data)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 555, in data
    ret = super().data
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 253, in data
    self._data = self.to_representation(self.instance)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 509, in to_representation
    attribute = field.get_attribute(instance)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\fields.py", line 490, in get_attribute
    raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `code` on serializer `UserOTPCodeSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `User` instance.      
Original exception text was: 'User' object has no attribute 'code'.
Internal Server Error: /user/register/verify-otp/
Traceback (most recent call last):
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\fields.py", line 457, in get_attribute
    return get_attribute(instance, self.source_attrs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\fields.py", line 97, in get_attribute
    instance = getattr(instance, attr)
AttributeError: 'User' object has no attribute 'code'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", 
line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", 
line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
480, in raise_uncaught_exception
    raise exc
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\views.py", line 
506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\generics.py", line 190, in post
    return self.create(request, *args, **kwargs)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\mixins.py", line 20, in create
    headers = self.get_success_headers(serializer.data)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 555, in data
    ret = super().data
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 253, in data
    self._data = self.to_representation(self.instance)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 509, in to_representation
    attribute = field.get_attribute(instance)
  File "C:\Users\61403\AppData\Local\Programs\Python\Python310\lib\site-packages\rest_framework\fields.py", line 490, in get_attribute
    raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `code` on serializer `UserOTPCodeSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `User` instance.      
Original exception text was: 'User' object has no attribute 'code'.
bvjxkvbb

bvjxkvbb1#

你必须使用write_only=True,就像这样:
code =序列化程序.IntegerField(write_only=True)

相关问题