Well, I have a custom User Model in an app called accounts. The accounts.User looks like this:
from django.contrib.auth.models import AbstractBaseUser
from django.core.validators import RegexValidator
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from accounts.managers import UserManager
from base.models import Model
from accounts.models.profiles import Profile
class User(Model, AbstractBaseUser):
objects = UserManager()
phone_regex = RegexValidator(
regex=r'^\+?1?\d{9,15}$',
message="Phone number must be entered in the format: '+999999999'. 9-15 digits allowed.",
)
phone = models.CharField(
validators=[phone_regex],
max_length=17,
blank=True
)
email = models.EmailField(
max_length=255,
unique=True,
)
active = models.BooleanField(
default=True
)
staff = models.BooleanField(
default=False
)
superuser = models.BooleanField(
default=False
)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_active(self):
return self.active
@property
def is_staff(self):
return self.staff
@property
def is_superuser(self):
return self.superuser
def serialize(self):
data = {
'id': self.id,
'email': self.email,
}
return data
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
and the UserManager looks like this:
from django.contrib.auth.models import BaseUserManager
class UserManager(BaseUserManager):
def create_user(self, email, password):
if not email:
raise ValueError('Users must have an email')
if not password:
raise ValueError('Users must have a password')
user = self.model(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, email, password):
user = self.create_user(
email=email,
password=password,
)
user.staff = True
user.save(using=self._db)
return user
def create_superuser(self, email, password):
user = self.create_user(
email=email,
password=password,
)
user.staff = True
user.superuser = True
user.save(using=self._db)
return user
And my accounts app registered like this:
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'base',
'accounts',
]
AUTH_USER_MODEL = 'accounts.User'
...
I run python manage.py makemigrations and python manage.py migrate and everything run smooth as expected. The problem happen when I start to python manage.py createsuperuser. It prompts to give an email input as expected but when typed an email and click enter it returns me an error.
Traceback (most recent call last):
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: accounts_user
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 59, in execute
return super().execute(*args, **options)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 117, in handle
self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 44, in get_by_natural_key
return self.get(**{self.model.USERNAME_FIELD: username})
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/models/query.py", line 397, in get
num = len(clone)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/models/query.py", line 254, in __len__
self._fetch_all()
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1064, in execute_sql
cursor.execute(sql, params)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/zulwiyozaputra/Developer/Environments/poros/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: accounts_user
What I'm trying is I tried to reproduce the error by deleting the database and doing the migration and I found there is no migration file created in my app as expected because it supposed to have migration file of my custom user created in the database. Anyone had this error before? And how did you resolve it?