title: Creating custom user model in middle of project without erasing migration or database
body: <p>In my models, I created UserManager and User and I added this in my settings:</p>
  
  <pre><code>AUTH_USER_MODEL = "myapp.User"
  </code></pre>
  
  <p>but when I migrate it raises error related model cannot be resolved.
  I searched the internet but I couldn't find the answer which solves my problem
  &lt;--->
  As there is no good answer on the internet for this problem and I still didn't find a solution for it with the help of a friend here(with all this I don't know I get minus point) I think I need to ask with more details
  I defined this classes in my models.py:</p>
  
  <pre><code>from __future__ import unicode_literals
  from django.db import models
  
  from django.db import models
  from django.utils import timezone
  from django.contrib.auth.models import (
      AbstractBaseUser, PermissionsMixin
  )
  
  from django.contrib.auth.models import (
      AbstractBaseUser, PermissionsMixin, BaseUserManager
  )
  
  
  class UserManager(BaseUserManager):
  
      def _create_user(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')
          try:
              with transaction.atomic():
                  user = self.model(email=email, **extra_fields)
                  user.set_password(password)
                  user.save(using=self._db)
                  return user
          except:
              raise
  
      def create_user(self, email, password=None, **extra_fields):
          extra_fields.setdefault('is_staff', False)
          extra_fields.setdefault('is_superuser', False)
          return self._create_user(email, password, **extra_fields)
  
      def create_superuser(self, email, password, **extra_fields):
          extra_fields.setdefault('is_staff', True)
          extra_fields.setdefault('is_superuser', True)
  
          return self._create_user(email, password=password, **extra_fields)
  
  
  class User(AbstractBaseUser, PermissionsMixin):
      """
      An abstract base class implementing a fully featured User model with
      admin-compliant permissions.
  
      """
      email = models.EmailField(max_length=40, unique=True)
      first_name = models.CharField(max_length=30, blank=True)
      last_name = models.CharField(max_length=30, blank=True)
      is_active = models.BooleanField(default=True)
      is_staff = models.BooleanField(default=False)
      date_joined = models.DateTimeField(default=timezone.now)
  
      objects = UserManager()
  
      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = ['first_name', 'last_name']
  
      def save(self, *args, **kwargs):
          super(User, self).save(*args, **kwargs)
          return self
  </code></pre>
  
  <p>and I added this in my settings:</p>
  
  <pre><code>AUTH_USER_MODEL = "myapp.User"
  </code></pre>
  
  <p>I run makemigrations and it had no erroe but when I try to run migrate I give this error:</p>
  
  <pre><code>in resolve_related_fields
      raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
  ValueError: Related model 'myapp.User' cannot be resolved
  </code></pre>