Thi*_*ker 3 django django-rest-framework django-allauth django-rest-auth
我在我的django项目中使用Django-rest-auth(https://github.com/Tivix/django-rest-auth)进行登录和注册.我看到一个默认的注册表格如下:
目前,我可以使用电子邮件而不是用户名注册新用户.我的MySql数据库中的默认auth_user表包含以下列:(id,password,last_login,is_superuser,username,first_name,last_name,email,is_staff,is_active,date_joined)
我的settings.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',
)
Run Code Online (Sandbox Code Playgroud)
我想修改我的注册表单,使其具有上面字段的名字和姓氏,这样当我注册一个新用户时,这两列也会填充first_name和last_name.目前我没有其他注册视图或任何自定义用户模型,我只是使用django-rest-auth提供的API端点.
我怎样才能做到这一点?
小智 8
您可以通过扩展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', '')
}
Run Code Online (Sandbox Code Playgroud)
在你的settings.py上添加:
REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'path.to.your.RegistrationSerializer'
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1210 次 |
| 最近记录: |