May, 2022 Update:
This instruction shows how to set up authentication with "email" and "password" instead of "username" and "password" and in this instruction, "username" is removed and I tried not to change the default Django settings as much as possible.
First, run the command below to create "account" application:
python manage.py startapp account
Then, set "account" application to "INSTALLED_APPS" and set AUTH_USER_MODEL = 'account.CustomUser' in "settings.py" as shown below:
# "settings.py"
INSTALLED_APPS = [
# ...
"account", # Here
]
AUTH_USER_MODEL = 'account.CustomUser' # Here
Then, create "managers.py" just under "account" folder and create "CustomUserManager" class extending "UserManager" class in "managers.py" as shown below. *Just copy & paste the code below to "managers.py" and "managers.py" is necessary to make the command "python manage.py createsuperuser" work properly without any error:
# "account/managers.py"
from django.contrib.auth.models import UserManager
from django.contrib.auth.hashers import make_password
class CustomUserManager(UserManager): # Here
def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError("The given email must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.password = make_password(password)
user.save(using=self._db)
return user
def create_user(self, email=None, 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=None, 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)
Then, create "CustomUser" class extending "AbstractUser" class and remove "username" by setting it "None" and set "email" with "unique=True" and set "email" to "USERNAME_FIELD" and set "CustomUserManager" class to "objects" in "account/models.py" as shown below. *Just copy & paste the code below to "account/models.py":
# "account/models.py"
from django.db import models
from django.contrib.auth.models import AbstractUser
from .managers import CustomUserManager
class CustomUser(AbstractUser):
username = None # Here
email = models.EmailField('email address', unique=True) # Here
USERNAME_FIELD = 'email' # Here
REQUIRED_FIELDS = []
objects = CustomUserManager() # Here
class Meta:
verbose_name = "custom user"
verbose_name_plural = "custom users"
Or, you can also create "CustomUser" class extending "AbstractBaseUser" and "PermissionsMixin" classes as shown below. *This code below with "AbstractBaseUser" and "PermissionsMixin" classes is equivalent to the code above with "AbstractUser" class and as you can see, the code above with "AbstractUser" class is much less code:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.utils import timezone
from .managers import CustomUserManager
class CustomUser(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField("first name", max_length=150, blank=True)
last_name = models.CharField("last name", max_length=150, blank=True)
email = models.EmailField('email address', unique=True)
is_staff = models.BooleanField(
"staff status",
default=False,
help_text="Designates whether the user can log into this admin site.",
)
is_active = models.BooleanField(
"active",
default=True,
help_text=
"Designates whether this user should be treated as active. "
"Unselect this instead of deleting accounts."
,
)
date_joined = models.DateTimeField("date joined", default=timezone.now)
USERNAME_FIELD = 'email'
objects = CustomUserManager() # Here
Then, create "forms.py" just under "account" folder and create "CustomUserCreationForm" class extending "UserCreationForm" class and "CustomUserChangeForm" class extending "UserChangeForm" class and set "CustomUser" class to "model" in "Meta" inner class in "CustomUserCreationForm" and "CustomUserChangeForm" classes in "account/forms.py" as shown below. *Just copy & paste the code below to "forms.py":
# "account/forms.py"
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = ('email',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('email',)
Then, create "CustomUserAdmin" class extending "UserAdmin" class and set "CustomUserCreationForm" and "CustomUserChangeForm" classes to "add_form" and "form" respectively and set "AdminPasswordChangeForm" class to "change_password_form" then set "CustomUser" and "CustomUserAdmin" classes to "admin.site.register()" in "account/admin.py" as shown below. *Just copy & paste the code below to "account/admin.py":
from django.contrib import admin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from django.contrib.auth.forms import AdminPasswordChangeForm
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
fieldsets = (
(None, {"fields": ("password",)}),
("Personal info", {"fields": ("first_name", "last_name", "email")}),
(
"Permissions",
{
"fields": (
"is_active",
"is_staff",
"is_superuser",
"groups",
"user_permissions",
),
},
),
("Important dates", {"fields": ("last_login", "date_joined")}),
)
add_fieldsets = (
(
None,
{
"classes": ("wide",),
"fields": ("email", "password1", "password2"),
},
),
)
add_form = CustomUserCreationForm # Here
form = CustomUserChangeForm # Here
change_password_form = AdminPasswordChangeForm # Here
list_display = ("email", "first_name", "last_name", "is_staff")
list_filter = ("is_staff", "is_superuser", "is_active", "groups")
search_fields = ("first_name", "last_name", "email")
ordering = ("email",)
filter_horizontal = (
"groups",
"user_permissions",
)
admin.site.register(CustomUser, CustomUserAdmin) # Here
Then, run the command below to make migrations and migrate:
python manage.py makemigrations && python manage.py migrate
Then, run the command below to create a superuser:
python manage.py createsuperuser
Then, run the command below to run a server:
python manage.py runserver 0.0.0.0:8000
Then, open the url below:
http://localhost:8000/admin/login/
Finally, you can log in with "email" and "password" as shown below:
![enter image description here]()
And this is "Add custom user" page as shown below:
![enter image description here]()