创建方法在序列化器中引发关键错误

时间:2019-09-03 06:15:18

标签: django django-rest-framework

我正在创建用于注册的API。

Serializers.py

class UserSignupSerializer(serializers.Serializer):


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

def create(self, validate_data):

    user = User.objects.create(email=validate_data['email'], first_name=validate_data['first_name'],last_name=validate_data['last_name'], role='user', username=validate_data['username'])
    user.set_password(validate_data['password'])

    user.save()
    return user

Views.py

class UserSignupView(APIView):
    def post(self, request):
        serializer = UserSignupSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(status=status.HTTP_204_NO_CONTENT)

但是,这给出了关键错误“电子邮件”或我在此行中首先输入的内容:

user = User.objects.create(email=validate_data['email'], first_name=validate_data['first_name'],last_name=validate_data['last_name'], role='user', username=validate_data['username'])

修改 错误

Internal Server Error: /api/user_signup/
Traceback (most recent call last):
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 497, in dispatch
    response = self.handle_exception(exc)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 457, in handle_exception
    self.raise_uncaught_exception(exc)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 468, in raise_uncaught_exception
    raise exc
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 494, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:\Django\FitnessProject\FitnessApp\fitness_api\views.py", line 187, in post
    serializer.save()
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\serializers.py", line 213, in save
    self.instance = self.create(validated_data)
  File "D:\Django\FitnessProject\FitnessApp\fitness_api\serializers.py", line 85, in create
    user = User.objects.create(email=validate_data['email'], first_name=validate_data['first_name'],last_name=validate_data['last_name'], role='user', username=validate_data['username'])
KeyError: 'email'
[2019-09-03 11:49:43,186] log: ERROR - Internal Server Error: /api/user_signup/
Traceback (most recent call last):
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 497, in dispatch
    response = self.handle_exception(exc)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 457, in handle_exception
    self.raise_uncaught_exception(exc)
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 468, in raise_uncaught_exception
    raise exc
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\views.py", line 494, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:\Django\FitnessProject\FitnessApp\fitness_api\views.py", line 187, in post
    serializer.save()
  File "D:\django\FitnessProject\fitness_venv\lib\site-packages\rest_framework\serializers.py", line 213, in save
    self.instance = self.create(validated_data)
  File "D:\Django\FitnessProject\FitnessApp\fitness_api\serializers.py", line 85, in create
    user = User.objects.create(email=validate_data['email'], first_name=validate_data['first_name'],last_name=validate_data['last_name'], role='user', username=validate_data['username'])
KeyError: 'email'
[03/Sep/2019 11:49:43] "POST /api/user_signup/?username=user_4&last_name=user&first_name=user&email=u@gmail.com&password=user&role=user HTTP/1.1" 500 18553

1 个答案:

答案 0 :(得分:0)

我认为您应该使用 serializers.Modelserializer 而不是 serializers.Serializer