Я могу получить TypeError
при вызове User.objects.create()
.
TypeError: User () получил неожиданный аргумент ключевого слова 'new_password'
Это Было бы здорово, если бы кто-нибудь мог понять, где я делаю что-то не так. заранее большое спасибо.
serializers.py:
class PasswordChangeSerializer(serializers.ModelSerializer):
old_password = serializers.CharField(write_only=True, required=True, max_length=30)
new_password = serializers.CharField(write_only=True, required=True, max_length=30)
confirm_password = serializers.CharField(write_only=True, required=True, max_length=30)
class Meta:
model = User
fields = ['old_password', 'new_password', 'confirm_password']
def validate(self, data):
old_password = data['old_password']
new_password = data['new_password']
confirm_password = data['confirm_password']
user_queryset = User.objects.all()
if user_queryset.exists() and user_queryset.count() == 1:
user_set = user_queryset.first()
if not user_set.check_password(old_password):
raise serializers.ValidationError({'old_password': 'Wrong password!'})
if confirm_password != new_password:
raise serializers.ValidationError({'password': 'Password must be confirmed correctly!'})
return data
def update(self, instance, validated_data):
instance.set_password(validated_data['new_password'])
instance.save()
return instance
views.py:
class PasswordChangeAPIView(APIView):
permission_classes = [IsAdminUser]
serializer_class = PasswordChangeSerializer
def post(self, request, format=None):
data = request.data
password_ser = PasswordChangeSerializer(data=data)
if password_ser.is_valid():
password_ser.save()
new_data = password_ser.data
return Response(new_data, status=HTTP_201_CREATED)
else:
return Response({"msg":"invalid user!"}, status=HTTP_400_BAD_REQUEST)