# models.py
class User(AbstractUser):
username = None
email = models.EmailField(unique=True, verbose_name=_("email"))
phone = models.CharField(unique=True, max_length=11, verbose_name=_("phone"))
...
USERNAME_FIELD = "email"
EMAIL_FIELD = "email"
...
class Employee(models.Model):
...
user = models.OneToOneField(
"user.User",
on_delete=models.CASCADE,
related_name="employee",
verbose_name=_("user"),
)
class Company(models.Model):
...
class Member(models.Model):
is_owner = models.BooleanField(default=False, verbose_name=_("owner"))
is_manager = models.BooleanField(default=False, verbose_name=_("manager"))
is_active = models.BooleanField(default=True)
user = models.OneToOneField(
"user.User",
on_delete=models.CASCADE,
related_name="member",
verbose_name=_("user"),
)
company = models.ForeignKey(
"user.Company",
on_delete=models.CASCADE,
related_name="members",
verbose_name=_("company"),
)
from __future__ import unicode_literals
from django.db import models
from django.core.mail import send_mail
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.utils.translation import ugettext_lazy as _
from .managers import UserManager
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
is_active = models.BooleanField(_('active'), default=True)
avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
def get_full_name(self):
'''
Returns the first_name plus the last_name, with a space in between.
'''
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
'''
Returns the short name for the user.
'''
return self.first_name
def email_user(self, subject, message, from_email=None, **kwargs):
'''
Sends an email to this User.
'''
send_mail(subject, message, from_email, [self.email], **kwargs)