Django rest auth扩展注册表

时间:2016-04-22 18:17:15

标签: django django-rest-framework django-rest-auth

我正在使用Django-rest-auth进行身份验证和注册。我正在使用电子邮件以密码登录用户。 我想在注册时存储用户的名字和姓氏 (可能还有更多字段),即在注册表单的客户端会有以下字段(电子邮件,密码) ,名字,姓氏等。)。目前注册表只有电子邮件,密码1和密码2字段,注册基本上是成功的。

enter image description here

我的settings.py有以下内容:

#This is required otherwise it asks for email server
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True   
ACCOUNT_USERNAME_REQUIRED = False

#Following is added to enable registration with email instead of username
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",

#`allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend", 
)

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

如果您想扩展注册表单,则需要创建自己的包含更多字段的自定义用户。

所以在你的模型文件中有这个

from django.contrib.auth.models import (
   AbstractBaseUser, PermissionsMixin,
)

class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
    first_name = models.TextField(default='')
    last_name = models.TextField(default='')
    #any other fields you want

    USERNAME_FIELD = 'email'

然后在你的设置中你想要这个

AUTH_USER_MODEL = 'app_name.CustomUser'

然后当您访问此表单页面时,django rest应该使用此用户模型

我希望这有帮助!

修改

我相信这会自动处理密码,因为它扩展了AbtractBaseUser,让我知道它是不是