我想修改我的
django用户模型以允许电话或电子邮件注册/登录.运用
USERNAME_FIELD = 'identifier'
如果用户使用电话号码注册,则标识符将是其电话号码或电子邮件,反之亦然. (如果有人认为我应该只指定一些号码作为标识符,请告诉我.)
这是我的accounts.models:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from phonenumber_field.modelfields import PhoneNumberField
class UserManager(BaseUserManager):
def create_user(self, email, phone, password, **kwargs):
"""
Creates and saves a Account with the given email or phone and password.
"""
now = timezone.now()
identifier = ''
if not email:
if not phone:
raise ValueError('Users must have a valid email or phone address.')
else:
identifier = phone
if not phone:
if not email:
raise ValueError('Users must have a valid email or phone address.')
else:
email = self.normalize_email(email)
identifier = email
user = self.model(email=email, phone=phone,
identifier=identifier,
joined=now,
**kwargs
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password, **kwargs):
user = self.model(
email=email,
is_staff=True,
is_superuser=True,
**kwargs
)
user.set_password(password)
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(null=True, blank=True, unique=True)
phone = PhoneNumberField(null=True, blank=True, unique=True)
joined = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=200, null=True, blank=True)
last_name = models.CharField(max_length=200, null=True, blank=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
identifier = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'
def get_username(self):
return self.email
def get_short_name(self):
return self.first_name
short_name = property(fget=get_short_name)
我不太确定这是否是接近用户模型的正确方法,而且我更加困惑于尝试将all-auth和rest-auth与它集成.现在,我的直觉告诉我要自己创造一切.如果任何人有任何与aut-auth集成的经验,那么它允许电话/电子邮件注册&登录,以及我是否应该尝试分叉/只是从头开始,请告诉我.
所以主要是这两个问题:
>我的用户模型可以改进吗?
>您对Allauth集成电话和电话的想法邮件验证.
最佳答案 我最终创建了一个用于存储和管理用户电话号码的新应用程序.
逻辑是,当用户使用电子邮件注册时,会定期进行全副本流程触发.当用户使用电话号码注册时,我使用自定义模型/视图/身份验证来记录用户,验证等.
以下是我的模特:
class User(AbstractBaseUser, PermissionsMixin):
email = EmailLoginField(blank=True, unique=True, null=True) # used as login as can't be null.
email2 = models.EmailField(blank=True, null=True) # for users who joined with phone and added email to emailfield.
phone = models.CharField(max_length=30, null=True, blank=True)
—在phonenumber应用程序内部
class PhoneNumber(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE)
phone = models.CharField(unique=app_settings.UNIQUE_PHONE, max_length=254, verbose_name=_('phone number'))
verified = models.BooleanField(verbose_name=_('verified'), default=False)
primary = models.BooleanField(verbose_name=_('primary'), default=False)
class PhoneConfirmation(models.Model):
phone_number = models.ForeignKey(PhoneNumber, verbose_name=_('phone number'))
created = models.DateTimeField(verbose_name=_('created'), default=timezone.now)
sent = models.DateTimeField(verbose_name=_('sent'), null=True)