简体   繁体   English

Django扩展AbstractUser并在其他字段中将email字段用作默认值

[英]Django Extend AbstractUser and use email Field as default in other field

I'm using Django 1.9.2 with python 2.7.3, rest framework and allauth. 我正在将Django 1.9.2与python 2.7.3,rest framework和allauth一起使用。 I'm extending from django.contrib.auth.models.AbstractUser and I want to get the email field from AbstractUser and use it as default in other field: 我从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())

But when I use this code, I get the following error: 但是,当我使用此代码时,出现以下错误:

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)

What is the way for get the email attribute? 如何获取电子邮件属性?

You cannot extend AbstractUser for this purpose. 您不能为此扩展AbstractUser Extend AbstractBaseUser for this. 为此扩展AbstractBaseUser Inherit PermissionsMixin , if you want to use those features. 如果要使用这些功能,请继承PermissionsMixin And also make a custom manager extending BaseUserManager . 并制作一个扩展BaseUserManager的自定义管理器。

Example - 范例-

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

Thanks to RA123 for the orientation, I have also overwritten the save method of MyUser and instead of implementing my own UserManager, I have implemented the default and I have added the necessary fields: 多亏了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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM