Got AttributeError 'User' object has no attribute 'password1'

How to fix the error please help?Got AttributeError 'User' object has no attribute 'password1'.

i want to add two password fields. user is created but error occurs

AttributeError: Got AttributeError when attempting to get a value for field password1 on serializer UserSerializer. The serializer field might be named incorrectly and not match any attribute or key on the User instance. Original exception text was: 'User' object has no attribute 'password1'.

views.py

class RegisterAPIView(generics.CreateAPIView):
    serializer_class = UserSerializer
    queryset = User.objects.all()

serializers.py

class UserSerializer(serializers.Serializer):
    username = serializers.CharField(
        label="Имя пользователя",
        style={"input_type": "username"}
    )
    email = serializers.CharField(
        label="почта",
        style={"input_type": "email"}
    )
    password1 = serializers.CharField(
        label="Пароль",
        style={'input_type': 'password'}
    )
    password2 = serializers.CharField(
        label="Пароль повторно",
        style={'input_type': 'password'}
    )

    def create(self, validated_data):
        password1 = validated_data.pop('password1')
        password2 = validated_data.pop('password2')
        print(validated_data)
        if password1 and password2 and password1 != password2:
            raise ValidationError("Passwords don't match")
        validated_data["password"] = password1
        print(validated_data)
        user = User.objects.create(**validated_data)
        print(user.id)
        return user
Back to Top