我在我的django项目中使用Django-rest-auth(https://github.com/tivix/Django-rest-auth)进行登录和注册。我看到一个默认的注册表单,如下所示:
目前,我可以注册一个新的用户与电子邮件,而不是用户名。my MySql数据库中默认的auth_user表有以下列:(id、密码、last_login、is_superuser、username、first_name、last_name、email、is_staff、is_active、date_joined)
我的解决方案。PY:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'rest_framework',
#Rest-auth
'rest_framework.authtoken',
'rest_auth',
#Rest-auth registration
'allauth',
'allauth.account',
'rest_auth.registration',
#Following is added to allow cross domain requests i.e. for serving requests those are coming from frontend app
'corsheaders',
'flights',
)
您可以通过扩展REST-AUTH的RegisterSerializer来实现这一点
from rest_auth.registration.serializers import RegisterSerializer
class RegistrationSerializer(RegisterSerializer):
first_name = serializers.CharField(required=True)
last_name = serializers.CharField(required=True)
def get_cleaned_data(self):
return {
'first_name': self.validated_data.get('first_name', ''),
'last_name': self.validated_data.get('last_name', ''),
'username': self.validated_data.get('username', ''),
'password1': self.validated_data.get('password1', ''),
'email': self.validated_data.get('email', '')
}
在您的解决方案中。py添加以下内容:
REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'path.to.your.RegistrationSerializer'
}