Hello everyone, I’m new to Django. I have an issue where, inside the admin area, the custom User (no username, login using email) that I made isn’t grouped under “Authentication and Authorization”, here is a screenshot:
And here are my relevant code snippets:
settings.py
AUTH_USER_MODEL = 'some_app_with_custom_user.User'
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User
class UserAdmin(BaseUserAdmin):
ordering = ('email',)
admin.site.register(User, UserAdmin)
models.py
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractUser
)
class UserManager(BaseUserManager):
"""
Custom user model manager where email is the unique identifier
for authentication instead of usernames.
"""
def create_user(self, email, password=None):
"""
Creates and saves a user with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(
email,
password=password,
)
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
class User(AbstractUser):
class Meta:
db_table = 'auth_user'
username = None
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email
I’d like Users
and Groups
to stay together, am I doing something wrong?