Reputation: 8360
I've defined a custom user model:
class User(AbstractUser):
REQUIRED_FIELDS = []
USERNAME_FIELD = 'email'
email = models.EmailField(
_('email address'),
max_length=150,
unique=True,
help_text=_('Required. 150 characters or fewer. Must be a valid email address.'),
error_messages={
'unique':_("A user with that email address already exists."),
},
)
The point of it being to use an email address instead of username.
I've put this in settings:
# custom user model
AUTH_USER_MODEL = 'workoutcal.User'
The user model works, but there's one problem. I can't create superusers:
(workout) n155-p250:workout sahandzarrinkoub$ ./manage.py createsuperuser
Email address: [email protected]
Password:
Password (again):
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/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/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 59, in execute
return super().execute(*args, **options)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 179, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'username'
Seems like the create_superuser
method used has username as a required argument. I've read in the docs that I need to implement my own CustomUserManager
. I've already done so here:
class CustomUserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('email must be set')
email = self.normalize_email(email)
user = User(email = email, is_staff=is_staff,
is_superuser=is_superuser, date_joined=now,
**extra_fields)
user.set_password(password)
user.save()
return user
def create_user(self, email, password, **extra_fields):
return self._create_user(email, password, False, False, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
return self._create_user(email, password, True, True, **extra_fields)
It seems clear that my CustomUserManager
isn't managing the User
model. There seems to be no documentation telling me where to actually set the CustomUserManager
as the actual manager for the User
model. Could somebody tell me how to do that?
Upvotes: 4
Views: 446
Reputation: 8209
You also need to tell the new user to use the new manager:
class User(AbstractUser): # in your custom user
#...
objects = CustomUserManager()
#...
Django does the same thing in the source code
As for:
There seems to be no documentation telling me where to actually set the CustomUserManager as the actual manager for the User model.
Yes, there is actually a nice doc for that too, but explained in a more general way: https://docs.djangoproject.com/en/2.0/topics/db/managers/
Upvotes: 5