text: # Django core
  from django.db import models
  from django.contrib.auth.models import AbstractBaseUser
  from django.contrib.auth.models import PermissionsMixin
  from django.contrib.auth.base_user import BaseUserManager
  from django.dispatch import receiver
  from django.db.models.signals import post_save
  
  
  class UserManager(BaseUserManager):
      use_in_migrations = True
  
      def _create_user_with_email(self, email, password, **extra_fields):
          """
          Creates and saves a User with the given email and password.
          """
          if not email:
              raise ValueError('The given email must be set')
          email = self.normalize_email(email)
          user = self.model(email=email, **extra_fields)
          user.set_password(password)
          try:
              user.save(using=self._db)
              return user
          except Exception:
              raise ValueError('Duplicate email')
  
      def create_user(self, email, password=None, **extra_fields):
          extra_fields.setdefault('is_superuser', False)
          extra_fields.setdefault('is_staff', False)
          return self._create_user_with_email(email, password, **extra_fields)
  
      def create_superuser(self, email, password, **extra_fields):
          extra_fields.setdefault('is_superuser', True)
          extra_fields.setdefault('is_staff', True)
          return self._create_user_with_email(email, password, **extra_fields)
  
  
  class User(AbstractBaseUser, PermissionsMixin):
      email = models.EmailField('Email', unique=True, null=True)
      date_joined = models.DateTimeField('date joined', auto_now_add=True)
      is_active = models.BooleanField('active', default=True)
      is_staff = models.BooleanField('staff status', default=False)
  
      objects = UserManager()
  
      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = []
  
      class Meta:
          verbose_name = 'пользователь'
          verbose_name_plural = 'пользователи'
  
      def __str__(self):
          if not self.email:
              return u'No email, id:{}'.format(self.pk)
          else:
              return self.email
  
      def get_full_name(self):
          full_name = '%s %s' % (self.profile.first_name, self.profile.last_name)
          return full_name.strip()
  
      def get_short_name(self):
          return self.profile.first_name
  
  
  class Profile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      first_name = models.CharField('first name', max_length=30, blank=True)
      last_name = models.CharField('last name', max_length=30, blank=True)
      birth_date = models.DateField(null=True, blank=True)
  
  
  @receiver(post_save, sender=User)
  def create_or_update_user_profile(sender, instance, created, **kwargs):
      if created:
          Profile.objects.create(user=instance)
      instance.profile.save()
  
  