Skip to content

Instantly share code, notes, and snippets.

@BrockHerion
Created August 27, 2020 00:11
Show Gist options
  • Save BrockHerion/4a2485b42bddf69b05d015788c30e891 to your computer and use it in GitHub Desktop.
Save BrockHerion/4a2485b42bddf69b05d015788c30e891 to your computer and use it in GitHub Desktop.
Create a custom user manager
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
class CustomUserManager(BaseUserManager):
"""
Custom user model where the email address is the unique identifier
and has an is_admin field to allow access to the admin app
"""
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError(_("The email must be set"))
if not password:
raise ValueError(_("The password must be set"))
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_active', True)
extra_fields.setdefault('role', 1)
if extra_fields.get('role') != 1:
raise ValueError('Superuser must have role of Global Admin')
return self.create_user(email, password, **extra_fields)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment