Search code examples
djangodjango-rest-frameworkdjango-formsdjango-viewsdjango-serializer

django rest Invalid password format or unknown hashing algorithm


User = get_user_model()

class CreateUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','phone' , 'password',)
        write_only_fields = ('password',)

        def create(self, validated_data):
            user = User.objects.create(validated_data['phone'])
            user.set_password(validated_data['password'])
            user.save()
            return user



        })


class Register(APIView):

    def post(self, request, *args, **kwargs):
        phone = request.data.get('phone', False)
        password = request.data.get('password', False)
        print(phone)
        print(password)
        if phone and password:
            old = PhoneOTP.objects.filter(phone__iexact=phone)
            if old.exists():
                old = old.first()
                validated = old.validate
                if validated:
                    temp_data = {
                        'phone': phone,
                        'password': password
                    }
                    serializers = CreateUserSerializer(data=temp_data)
                    serializers.is_valid(raise_exception=True)
                    user = serializers.save()

                    old.delete()
                    return Response({
                        'status': True,
                        'detail': 'Account is  created '
                    })

while saving user pasword feild is shows Invalid password format or unknown hashing algorithm. user is created the password feild is Invalid password format or unknown hashing algorithm.

uable to find y also tried user.set_unusable_password() in serializer but same result could not figure it out


Solution

  • I try a lot a method you are using. It didn't save the password in the right form. I suggest you create another model such as Profile and serializers for it and then try this -:

    views.py

    class Register(APIView):
        
        permission_classes = (AllowAny, )
        serializer_class = UserRegistrationSerializer
        def post(self, request, *args, **kwargs):
            phone = request.data.get('phone' , False)
            if phone:
                old = PhoneOTP.objects.filter(phone__iexact = phone)
                if old.exists():
                    old  = old.last()
                    validated = old.validated
    
                    if validated:
                       
                        serializer = self.serializer_class(data=request.data)
                        serializer.is_valid(raise_exception=True)
                        serializer.save()
                        response = {
                            'success' : 'True',
                            'status code' : status.HTTP_200_OK,
                            'message': 'User registered  successfully',
                            }
                        status_code = status.HTTP_200_OK
                        return Response(response, status=status_code)
            
                    else:
                        return Response({
                            'status' : False,
                            'detail' : "OTP haven't verified. FIrst do that step."
                            })
                else:
                    return Response({
                        'status' : False,
                        'detail' : 'Please verify phone number first.'
                        })
    
    
            else:
                return Response({
                    'status' : False,
                    'detail' : 'Phone password, address, Date_Of_Birth,  are not sent.'
                    })
    

    serializers.py

    class UserRegistrationSerializer(serializers.ModelSerializer):
    
        profile = ProfileSerializer(required=False)
    
        class Meta:
            model = User
            fields = ('phone', 'username', 'password', 'profile')
            extra_kwargs = {'password': {'write_only': True}}
    
        def create(self, validated_data):
            profile_data = validated_data.pop('profile')
            user = User.objects.create_user(**validated_data)
            users = Profile.objects.create(
                user=user,      
                state=profile_data['state'],
                city=profile_data['city'],
                date_Of_Birth=profile_data['date_Of_Birth'],
                address=profile_data['address']
                
            )
            users.save()
            
            return users
    

    Hope the answer is useful