Search code examples
djangoauthenticationdjango-rest-frameworkdjango-allauth

Overriding Sign Up functionality of django-rest-auth or django-allauth


I want to override how django-rest-auth signs up a user. By default following info is needed to sign up a user :

{
    "username" : "someusername",
    "password1" : "somepassword",
    "password2" : "somepassword",
    "email"     : "someemail"
}

I want to customise it to be :

  {
        "username" : "someusername",
        "HOD_email" : "somepassword",
        "phone_number" : "somepassword",
        "email"     : "someemail",
        "emp_id"    : "some_emp_id"
    }

Also I want to create a random dummy password for every new user on sign up. Here is my current code :

# serializers.py
5class RegistrationSerializer(serializers.ModelSerializer):                                            
 6                                                                                                      
 7    class Meta:                                                                                       
 8        model = User                                                                                  
 9        fields = ('username', 'email', 'phone_number', 'emp_id', 'HOD_email')                         
10                                                                                                      
11                                                                                                      
12        def save(self, request):                                                                      
13            """                                                                                       
14            Create and return a Token                                                                 
15            """                                                                                       
16            print("My create is getting called------")                                                
17            user = User(**validated_data)                                                             
18            try:                                                                                      
19                user.save()                                                                           
20            except Exception as e:                                                                    
21                print("error creating user", e)                                                       
22            return user        

# settings.py
36# REST AUTH SETTINGS                                                                                 
237REST_AUTH_SERIALIZERS = {                                                                            
238    'REGISTER_SERIALIZER' : 'home.detectSoftware.detect.projects.GUMPS_BE_DEV_V2.1.Server.UserAuth.s\
   erializers.RegistrationSerializer'                                                                   
239}                                                                                                    

However, it continues to use the existing functionality.


Solution

  • the name that you used for the setting dict is invalid, change it to this:

    REST_AUTH_REGISTER_SERIALIZERS = {                                                                            
        'REGISTER_SERIALIZER': 'home.detectSoftware.detect.projects.GUMPS_BE_DEV_V2.1.Server.UserAuth.serializers.RegistrationSerializer'                                                                   
     }
    

    read the document for more information.