Django休息注册 [英] Django rest registration

查看:269
本文介绍了Django休息注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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
'django.contrib.sites',
'rest_framework',
#Rest-auth
'rest_framework.authtoken',
'rest_auth'
#Rest-auth注册
'allauth',
'allauth.account',
'rest_auth.registration',
#以下是添加以允许跨域请求即,要求服务请求来自前端app
'corsheaders',
'flights',

我想修改我的注册表单以名字和姓氏使用以上字段,以便当我注册一个新用户时,这两列也填充了first_name&姓。目前我没有额外的注册视图,也没有任何自定义用户模型,我只是使用django-rest-auth提供的API端点。



如何实现?

解决方案

您可以通过从rest_auth.registration.serializers扩展rest-auth的RegisterSerializer

  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','')
}

和你的ettings.py添加以下内容:

  REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER':'path.to.your.RegistrationSerializer'
}


I am using Django-rest-auth (https://github.com/Tivix/django-rest-auth) in my django project for login and registration. I see a default registration form as follows:

Currently I am being able to register a new user with email instead of username. The default auth_user table in my MySql database has following columns: (id, password,last_login, is_superuser, username, first_name, last_name, email, is_staff, is_active, date_joined)

My 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',
)

I want modify my registration form to have first name and last name with above fields so that when I register a new user, those two columns are also populated with first_name & last_name. Currently I have no additional view for registration nor any custom user model, I am simply using API endpoints provided by django-rest-auth.

How can I achieve this?

解决方案

You can achieve that by extending rest-auth's 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', '')
        }

And on your settings.py add this:

REST_AUTH_REGISTER_SERIALIZERS = {
    'REGISTER_SERIALIZER': 'path.to.your.RegistrationSerializer'
}

这篇关于Django休息注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆