Reputation: 2126
So I changed the Django username field to 'email', which is documented as completely legal here, but after this, the createsuperuser
method doesn't work. Of cause I have added the Auth model to my settings and it worked before. This is basically my model currently:
class User(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
After migrating the change createsuperuser
throws this after every input is entered:
XXX\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'username'
I tried moving around this with a custom manager, but it threw new problems plus I would really would like to avoid this. Furthermore, I looked at the source code of the method, but couldn't figure out where this could go wrong. I also reset and deleted the database without luck.
Upvotes: 2
Views: 631
Reputation: 2110
It is happening because you should've overridden user model manager since you've changed the username field. For instance:
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""
Create and save a user with the given email, and password.
"""
if not email:
raise ValueError('The given email must be set')
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
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)
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)
And I think using this manager as your default manager will solve your problem. Because the default manager always looks for username field when it's creating a new instance. So you should also change your model like following:
class User(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
objects = UserManager() # use the above manager here
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
I hope this will solve your problem.
Upvotes: 2