Django Rest Framework New User Registration with Admin activation

67 Views Asked by At

in the old Django times I was able to set the is_activated hidden field per registration of the user data to let the new user to registrate but not able tro login. An admin needed to activate the account - perfect. With the django rest framework I can't find a possibility to do the same action. Can I get a hind please .... thank you very much.

Other maybe stupid question of this - how can I prevent the user from login without validation ( not Email validation )

Thanks a lot

Greetings RR

I tried successfully to add a user with all the methods I found online but always is_active was true Also it looked that the framework is always taking this field as true because its an admin feature to deactivate accounts - but this is the same I like to do.

Here what I have sofar: in views.py

class RegisterApi(generics.GenericAPIView):
    serializer_class = RegisterSerializer
    def post(self, request, *args,  **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        return Response({
            "user": UserSerializer(user,    context=self.get_serializer_context()).data,
            "message": "User Created Successfully.  Now perform Login to get your token",
        })

and serializer.py

class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','username','password')
        extra_kwargs = {
            'password':{'write_only': True}
        }
        def create(self, validated_data):
            user = User.objects.create_user(validated_data['username'],     password = validated_data['password'] )
            user.is_active = False  # is not working !!!!!!!!!!!
            return user


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
        extra_kwargs = {
            'password':{
                'write_only': True
            }
        }


There is no error when I set the python user.is_active = False in the user.create function of the ModelSerializer but the response and the user has still True ...

0

There are 0 best solutions below