本文介绍了Django-rest-auth(dj-rest-auth)自定义用户注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dj-rest-auth( https://dj-rest-auth.readthedocs.io/en/latest/),并尝试实施自定义注册表格.当我尝试注册新用户时,我具有基本表单.我见过较旧的版本( https://django-rest-auth.请阅读thedocs.io/en/latest/),如果您使用password1和password2,则不必重新输入所有代码.

I'm using dj-rest-auth (https://dj-rest-auth.readthedocs.io/en/latest/) and trying to implement a custom registration form. When I'm trying to register a new user I have the base form.I've seen with the older version (https://django-rest-auth.readthedocs.io/en/latest/) that if you use password1 and password2, you don't have to retype all the code.

serializers.py

serializers.py

from rest_framework import serializers
from dj_rest_auth.registration.serializers import RegisterSerializer


class CustomRegisterSerializer(RegisterSerializer):
first_name = serializers.CharField()
last_name = serializers.CharField()

def get_cleaned_data(self):
    super(CustomRegisterSerializer, self).get_cleaned_data()
    return {
        'username': self.validated_data.get('username', ''),
        'password1': self.validated_data.get('password1', ''),
        'password2': self.validated_data.get('password2', ''),
        'email': self.validated_data.get('email', ''),
        'first_name': self.validated_data.get('first_name', ''),
        'last_name': self.validated_data.get('last_name', '')
    }

settings.py

settings.py

REST_AUTH_SERIALIZERS = {
'REGISTER_SERIALIZER': 'accounts.serializers.CustomRegisterSerializer',
}

推荐答案

问题出在settings.py:

The problem was in settings.py:

REST_AUTH_REGISTER_SERIALIZERS = {
    'REGISTER_SERIALIZER': 'accounts.serializers.CustomRegisterSerializer'
}

这篇关于Django-rest-auth(dj-rest-auth)自定义用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:12