我正在使用django rest框架、注册和登录。我收到了这个错误,“clientname”对象没有属性“set\u password”如何解决这个问题
这是我的serializers.py
class clientnameSerializer(serializers.ModelSerializer):
password2 = serializers.CharField(style={'input_type': 'password'}, write_only=True)
class Meta:
model = clientname
fields = ('id','username','password', 'password2' , 'email')
ordering = ['id',]
extra_kwargs = {
'password': {'write_only': True}
}
def save(self):
account = clientname(
email=self.validated_data['email'],
username=self.validated_data['username'],
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
if password != password2:
raise serializers.ValidationError({'password': 'Passwords must match.'})
account.set_password(password)
account.save()
return account
这是我的观点
@api_view(['POST',])
def registration_view(request):
if request.method == 'POST':
serializer = clientnameSerializer(data=request.data)
data={}
if serializer.is_valid():
account = serializer.save()
data['response'] = "successfully registered a new user."
data['email'] = account.email
data['username'] = account.username
else:
data = serializer.errors
return Response(data)
这是我的全部回溯
回溯:
File "/home/schoolsite/schoolsitedir/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
54. return view_func(*args, **kwargs)
File "/home/schoolsite/schoolsitedir/env/lib/python3.6/site-packages/django/views/generic/base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "/home/schoolsite/schoolsitedir/env/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
505. response = self.handle_exception(exc)
File "/home/schoolsite/schoolsitedir/env/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
465. self.raise_uncaught_exception(exc)
File "/home/schoolsite/schoolsitedir/env/lib/python3.6/site-packages/rest_framework/views.py" in raise_uncaught_exception
476. raise exc
File "/home/schoolsite/schoolsitedir/env/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
502. response = handler(request, *args, **kwargs)
File "/home/schoolsite/schoolsitedir/env/lib/python3.6/site-packages/rest_framework/decorators.py" in handler
50. return func(*args, **kwargs)
File "/home/schoolsite/schoolsitedir/Homepage/api/views.py" in registration_view
25. account = serializer.save()
File "/home/schoolsite/schoolsitedir/Homepage/api/serializers.py" in save
30. account.set_password(password)
这是我的客户名型号
class clientname(models.Model):
username = models.CharField(max_length=500, null=True,blank=True)
password = models.CharField(max_length=500, null=True,blank=True)
email = models.CharField(max_length=500, null=True,blank=True)
set_password()
是来自DjangoUser
模型的方法。请参见here,如果要使用自定义用户,则必须创建扩展Django用户模型的自定义用户模型注意:this如果您正在寻找自定义用户模型,则可能会对您有所帮助
相关问题 更多 >
编程相关推荐