简体   繁体   中英

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. 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:

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. Extend AbstractBaseUser for this. Inherit PermissionsMixin , if you want to use those features. And also make a custom manager extending 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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