Django CustomUser Model Causes Lazy Reference Errors During Migration
I'm working on a Django project where I have defined a CustomUser model in an app called authentication. I've correctly set the AUTH_USER_MODEL to reference this model, but when I try to run migrations, I get the following errors:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.Device.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.MoodReview.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.PasswordResetCode.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authtoken.Token.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
# authentication/models.py
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.db import models
from django.utils import timezone
class CustomUserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError("The Email field must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(email, password, **extra_fields)
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
name = models.CharField(max_length=255)
# ... other fields
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def __str__(self):
return self.email
**Updated configuration
**
AUTH_USER_MODEL = 'authentication.CustomUser'
**INSTALLED_APPS **
INSTALLED_APPS = [
'authentication',
'django.contrib.admin',
'django.contrib.auth',
# ... other apps
]
Have you tried making migrations before specifying your custom user model as AUTH_USER_MODEL
in settings.py
?
Seems like you might be in the situation that has been covered here: https://stackoverflow.com/a/53936097/27487442