Custom User model in admin area

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:

image

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?

No, you’re not doing anything wrong - this is just how the admin works.

The Group table is in a different app than your User table, and so the Admin shows them as being members of their respective apps.

Something you might try that could work -

  • Deregister the default Group admin class
  • Create your own ModelAdmin class in your app that references the auth Group model
    (This is just an off-the-cuff idea - I have no idea whether or not this is going to do any good whatsoever.)

Beyond that, I’d be curious to find out why this is an issue. (If it’s just because you think it looks messy, that’s ok. But if there are other reasons, there may be more to my answer than this.)

Ken

1 Like

Thanks for your answer. It’s just more disorganized than the default (feels weird not to have auth users under AuthN & AuthZ). I’ll just live with it for now.

Is there something else other than custom User that’s difficult to modify down the line?

Not that I’m aware of. The issue with the User object is that it’s tied so tightly to so many other components that changing it is a really fundamental issue affecting many things that you might not think would be affected.
Beyond that, there are some other fundamental entities that would be difficult to change, but they’re the type of object that I can’t see ever wanting or needing to change.

1 Like

In my case (email for username), is there a workaround without modifying the User at all?

If that’s the only change you’re needing to make, then I would suggest using one of the existing packages that support this. In this case, there’s a fairly well known package, django-username-email, that might suit your purpose.

Whenever you’re looking for the type of functionality that you think may be more common than just your usage, check out djangopackages.org. Now, I don’t recommend blindly selecting packages and using them without a little bit of research, but they will at least give you some idea as to how other people approach finding solutions to requirements similar to what you might be facing.

Ken

1 Like

Thanks, you’ve been very insightful.