shoaibdevs
shoaibdevs

Reputation: 399

Invalid password format or unknown hashing algorithm. in django rest-framework

After saving the user password field, the app shows an Invalid password format or unknown hashing algorithm.

User is created, but the password field is Invalid password format or unknown hashing algorithm.

The code does not return any error during registration.

I also tried user.set_unusable_password() in serializer but with the same result. Could not figure it out.

Serializers.py

from rest_framework import serializers
from accounts.models import User

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

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

        })

view.py



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 '
                    })


Upvotes: 1

Views: 292

Answers (1)

shoaibdevs
shoaibdevs

Reputation: 399

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

views.py

class UserRegistrationView(CreateAPIView):

    serializer_class = UserRegistrationSerializer
    permission_classes = (IsAuthenticated,)

    def post(self, request):
        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)

Hope The answer is useful.

Upvotes: 1

Related Questions