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'.

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

class UserSerializer(serializers.Serializer):
    username = serializers.CharField(
        label="Имя пользователя",
        style={"input_type": "username"}
    email = serializers.CharField(
        style={"input_type": "email"}
    password1 = serializers.CharField(
        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')
        if password1 and password2 and password1 != password2:
            raise ValidationError("Passwords don't match")
        validated_data["password"] = password1
        user = User.objects.create(**validated_data)
        return user
Back to Top