django-rest-auth或django-allauth的覆盖注册功能

时间:2018-08-18 11:42:02

标签: django authentication django-rest-framework django-allauth

我想重写django-rest-auth如何注册用户。默认情况下,需要以下信息来注册用户:

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

我想将其自定义为:

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

我还想为每个注册新用户创建一个随机的虚拟密码。 这是我当前的代码:

# 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}                                                                                                    

但是,它继续使用现有功能。

1 个答案:

答案 0 :(得分:1)

您用于设置字典的名称无效,请将其更改为此:

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

阅读document以获得更多信息。