我正在注册新用户。这是它的模型:
from django.db import models
from django.contrib.auth.models import AbstractUser
from .validators import *
class User(AbstractUser):
status = models.CharField(
max_length=15,
choices=(
('C', 'Client'),
('M', 'Manager'),
('D', 'Director'),
),
default='C',
)
second_name = models.CharField(max_length=30, null=True, validators=[])
phone = models.CharField(max_length=30, null=True, unique=True, validators=[validate_phone])
class Client(User):
company = models.OneToOneField('Company', null=True)
loyalty = models.CharField(max_length=30, null=True)
requisites = models.OneToOneField('Requisites', null=True)
address = models.CharField(max_length=200, null=True)
curator = models.ForeignKey(
'Manager',
related_name='clients',
null=True,
)
为此,我有一个RegistrationView:
class RegistrationView(APIView):
def post(self, request):
serializer = RegistrationSerializer(data=request.data)
if serializer.is_valid():
new_user = serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
还有RegistrationSerializer:
class RegistrationSerializer(serializers.Serializer):
is_accepted = serializers.BooleanField(required=True)
first_name = serializers.CharField(required=True)
last_name = serializers.CharField(required=True)
email = serializers.EmailField(required=True)
password = serializers.CharField(write_only=True)
second_name = serializers.CharField(required=False)
company = CompanySerializer(required=False)
requisites = RequisitesSerializer(required=False)
address = serializers.CharField(required=False)
def validate_is_accepted(self, value):
if not value:
raise serializers.ValidationError('Вы не дали согласия на обработку персональных данных.')
return value
def create(self, validated_data):
data = validated_data.copy()
data.pop('is_accepted')
data['username'] = 'user' + str(User.objects.count())
data['is_active'] = False
return Client.objects.create_user(**data)
我使用is_accepted字段来确定接受新用户处理他的个人数据。显然,该字段不包含在用户模型中。它仅用于注册目的,因此我需要在创建新用户之前删除此字段。正如您所看到的,我在RegistrationSerializer的create方法中执行此操作。 但是,当我尝试注册新用户时,看起来is_accepted字段已被删除,因为我得到了下面的回溯。所以我使用日志记录检查了它确定 - is_accepted字段不包含在validated_data中,我将其作为参数放入create_user方法。它仅包含模型字段。所以我不明白这个错误。你能解释一下发生了什么吗?
AttributeError at /auth/registration/
Got AttributeError when attempting to get a value for field `is_accepted` on serializer `RegistrationSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Client` instance.
Original exception text was: 'Client' object has no attribute 'is_accepted'.
Request Method: POST
Request URL: http://95.213.203.11/auth/registration/
Django Version: 1.11.7
Python Executable: /root/ss/bin/uwsgi
Python Version: 3.6.3
Python Path: ['.', '', '/root/ss/lib/python36.zip', '/root/ss/lib/python3.6', '/root/ss/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6', '/root/ss/lib/python3.6/site-packages']
Traceback:
File "/root/ss/lib/python3.6/site-packages/rest_framework/fields.py" in get_attribute
441. return get_attribute(instance, self.source_attrs)
File "/root/ss/lib/python3.6/site-packages/rest_framework/fields.py" in get_attribute
100. instance = getattr(instance, attr)
During handling of the above exception ('Client' object has no attribute 'is_accepted'), another exception occurred:
File "/root/ss/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/root/ss/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/root/ss/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/root/ss/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/root/ss/lib/python3.6/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/root/ss/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
489. response = self.handle_exception(exc)
File "/root/ss/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
449. self.raise_uncaught_exception(exc)
File "/root/ss/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
486. response = handler(request, *args, **kwargs)
File "./auth/views.py" in post
18. return Response(serializer.data, status=status.HTTP_200_OK)
File "/root/ss/lib/python3.6/site-packages/rest_framework/serializers.py" in data
533. ret = super(Serializer, self).data
File "/root/ss/lib/python3.6/site-packages/rest_framework/serializers.py" in data
262. self._data = self.to_representation(self.instance)
File "/root/ss/lib/python3.6/site-packages/rest_framework/serializers.py" in to_representation
487. attribute = field.get_attribute(instance)
File "/root/ss/lib/python3.6/site-packages/rest_framework/fields.py" in get_attribute
462. raise type(exc)(msg)