Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django扩展AbstractUser并在其他字段中使用电子邮件字段作为默认字段_Python_Django_Django Models_Django Allauth_Django Rest Auth - Fatal编程技术网

Python Django扩展AbstractUser并在其他字段中使用电子邮件字段作为默认字段

Python Django扩展AbstractUser并在其他字段中使用电子邮件字段作为默认字段,python,django,django-models,django-allauth,django-rest-auth,Python,Django,Django Models,Django Allauth,Django Rest Auth,我将Django 1.9.2与python 2.7.3、rest框架和allauth一起使用。我是从django.contrib.auth.models.AbstractUser进行扩展的,我想从AbstractUser获取电子邮件字段,并在其他字段中使用它作为默认字段: from django.contrib.auth.models import AbstractUser class MyUser(AbstractUser): def get_email(self):

我将Django 1.9.2与python 2.7.3、rest框架和allauth一起使用。我是从django.contrib.auth.models.AbstractUser进行扩展的,我想从AbstractUser获取电子邮件字段,并在其他字段中使用它作为默认字段:

from django.contrib.auth.models import AbstractUser


class MyUser(AbstractUser):

    def get_email(self):
        return self.email

    email_from_work = models.EmailField(default=get_email())
但是,当我使用此代码时,会出现以下错误:

File "./behnowapp/models.py", line 48, in MyUser
    email_from_work = models.EmailField(default=get_email())
TypeError: get_email() takes exactly 1 argument (0 given)

获取电子邮件属性的方法是什么?

您不能为此扩展
AbstractUser
。为此扩展
AbstractBaseUser
。如果要使用这些功能,请继承
PermissionsMixin
。并创建一个自定义管理器,扩展
BaseUserManager

范例-

class MyUser(AbstractBaseUser, PermissionsMixin):

email = models.EmailField(max_length=255, unique=True)


objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []  # Fields necessary for making a user

def get_email(self):
    return self.email

您不能为此扩展
AbstractUser
。为此扩展
AbstractBaseUser
。如果要使用这些功能,请继承
PermissionsMixin
。并创建一个自定义管理器,扩展
BaseUserManager

范例-

class MyUser(AbstractBaseUser, PermissionsMixin):

email = models.EmailField(max_length=255, unique=True)


objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []  # Fields necessary for making a user

def get_email(self):
    return self.email

多亏了RA123的介绍,我还覆盖了MyUser的save方法,并没有实现我自己的UserManager,而是实现了默认值,并添加了必要的字段:

class MyUser(AbstractBaseUser, PermissionsMixin):

    def save(self, *args, **kwargs):
        if not self.email_from_work:
            self.email_from_work = self.get_email()
        super(MyUser, self).save(*args, **kwargs)

    def get_email(self):
        return self.email

    objects = UserManager()
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

    username = models.CharField(
        _('username'),
        max_length=30,
        unique=True,
        help_text=_('Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'),
        validators=[
            validators.RegexValidator(
                r'^[\w.@+-]+$',
                _('Enter a valid username. This value may contain only '
                  'letters, numbers ' 'and @/./+/-/_ characters.')
            ),
        ],
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('email address'), blank=True)
    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )
    date_joined = models.DateTimeField(_('date joined'), default=now)
    email_from_work = models.EmailField(max_length=255, unique=True)

多亏了RA123的介绍,我还覆盖了MyUser的save方法,并没有实现我自己的UserManager,而是实现了默认值,并添加了必要的字段:

class MyUser(AbstractBaseUser, PermissionsMixin):

    def save(self, *args, **kwargs):
        if not self.email_from_work:
            self.email_from_work = self.get_email()
        super(MyUser, self).save(*args, **kwargs)

    def get_email(self):
        return self.email

    objects = UserManager()
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

    username = models.CharField(
        _('username'),
        max_length=30,
        unique=True,
        help_text=_('Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'),
        validators=[
            validators.RegexValidator(
                r'^[\w.@+-]+$',
                _('Enter a valid username. This value may contain only '
                  'letters, numbers ' 'and @/./+/-/_ characters.')
            ),
        ],
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('email address'), blank=True)
    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )
    date_joined = models.DateTimeField(_('date joined'), default=now)
    email_from_work = models.EmailField(max_length=255, unique=True)

validators.RegexValidator的“代码”部分允许创建一个带有字符的用户名,例如
ñ
-
?它只允许创建带有“.”、“-”、“?”的用户名。您可以使用联机测试Regex表达式,并检查它是否与
验证程序的“代码”部分的表达式匹配。RegexValidator
允许使用
ñ
-
等字符创建用户名。它只允许创建带有“.”、“-”、”的用户名。您可以使用联机测试正则表达式,并检查它是否与表达式匹配