[英]user.save() not working properly when used in overriding serializer class's save() method
what i wanted was to send a post request with body containing email, username, password and password 2.我想要的是发送一个包含 email、用户名、密码和密码 2 的正文的发布请求。
the serializer class has a save method which has been overriden so that it checks password1 is equal to password2 or not.序列化程序 class 有一个保存方法,该方法已被覆盖,以便检查密码 1 是否等于密码 2。 if equal i wanted a user object to be created with email username and a password.如果相等,我希望使用 email 用户名和密码创建用户 object。 and wanted it to be saved.并希望它被保存。 im using User model of django.我正在使用 django 的用户 model。
error: TypeError: Got a TypeError
when calling User.objects.create()
.错误:TypeError:调用User.objects.create()
时出现TypeError
。 This may be because you have a writable field on the serializer class tha t is not a valid argument to User.objects.create()
.这可能是因为您在序列化程序 class 上有一个可写字段,这不是User.objects.create()
的有效参数。 You may need to make the field read-only, or override the RegistrationSerializer.create() method to handle this correctly.您可能需要将该字段设为只读,或重写 RegistrationSerializer.create() 方法以正确处理此问题。
Serializer Class:串行器 Class:
class RegistrationSerializer(serializers.ModelSerializer):
password2 = serializers.CharField(style={'input_type': 'password'}, write_only=True)
class Meta:
model = User
fields = ['email', 'username', 'password','password2']
extra_kwargs = {'password': {'write_only': True}}
# override one of its method to check if passwords match
def save(self):
user = User()
#cant create user py passing into constructor
user.email=self.validated_data['email']
user.username=self.validated_data['username']
password=self.validated_data['password']
password2=self.validated_data['password2']
if password!=password2:
raise serializers.ValidationError({'password':'password must match'})
user.set_password(password)
user.save()
return user
the view called:该视图称为:
@api_view(['POST', ])
def registration_view(request):
serializer=RegistrationSerializer(data=request.data)
data={}
if serializer.is_valid():
user=serializer.save()
data['response']="successfully created"
data['email']=user.email
data['username']=user.username
else:
data=serializer.errors
return Response(data)
As save method is not good place for validation, You should use validate function when you want control some fields are correct.由于保存方法不是验证的好地方,当您想要控制某些字段是否正确时,您应该使用 validate function。
class RegistrationSerializer(serializers.ModelSerializer):
password2 = serializers.CharField(style={'input_type': 'password'}, write_only=True)
class Meta:
model = User
fields = ['email', 'username', 'password','password2']
extra_kwargs = {'password': {'write_only': True}}
def validate(self, attrs):
if attrs.get('password') != attrs.get('password2'):
raise serializers.ValidationError({'password':'password must match'})
return attrs
def create(self, validated_data):
password2 = validated_data.pop('password2')
return super().create(validated_data)
If you want looks save function http://www.cdrf.co/3.9/rest_framework.serializers/ModelSerializer.html#save如果你想要看起来保存 function http://www.cdrf.co/3.9/rest_framework.serializers/ModelSerializer.html#save
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.