Get Django Custom user model listed under admin app `Authentication and Authorization`
Asked Answered
E

1

7

Note my question is similar to this one, however that answer recommend not calling the customised user model User, whereas the official docs and this issue do.

I created a custom User model in an app called plug:

plug/models.py:

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    """
    Requirements:
    - Must be defined in models.py, due to the way settings.AUTH_USER_MODEL is defined
    """
    is_hamster = models.BooleanField(default=False)
    is_superhero = models.BooleanField(default=False)
    is_pogo_stick = models.BooleanField(default=False)

    class Meta:
        # app_label = 'auth'  # <-- This doesnt work
        db_table = 'auth_user'

settings.py file set accordingly:

AUTH_USER_MODEL = 'plug.User'

plug/admin.py:

from django.contrib import admin

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import get_user_model

@admin.register(get_user_model())
class UserAdmin(UserAdmin):
    list_display = ('username', 'first_name', 'last_name', 'email', 'is_hamste', 'is_superhero', 'is_pogo_stick')
    list_display_links = list_display
    fieldsets = UserAdmin.fieldsets + (
        ('Leasing User Role', {'fields': ('is_hamste', 'is_superhero', 'is_pogo_stick')}),
)

All is good except in the admin interface the custom user is listed under the heading Plug instead of under Authentication and Authorization (where Users used to be listed, along with Groups).

I tried setting app_label = 'auth' in the meta of the custom user model, however then it fails with the error:

File "/home/michael/venv/project/lib/python3.8/site-packages/django/db/models/base.py", line 321, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "/home/michael/venv/project/lib/python3.8/site-packages/django/apps/registry.py", line 228, in register_model
    raise RuntimeError(
RuntimeError: Conflicting 'user' models in application 'auth': <class 'django.contrib.auth.models.User'> and <class 'dist.plug.models.User'>.

How does one get a custom user model to be listed under the default app admin heading of Authentication and Authorization ?

Enallage answered 6/8, 2021 at 11:33 Comment(2)
Did you find the answer to this?Coiffeur
@ShivanshJagga See my answer belowEnallage
E
4

It turns out the way is not adding your user model to the existing section, but rather remove groups from the existing section, and adding it to ones own section:

from django.contrib.auth.models import Group as DjangoGroup
from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin


class Group(DjangoGroup):
    """Instead of trying to get new user under existing `Aunthentication and Authorization`
    banner, create a proxy group model under our Accounts app label.
    Refer to: https://github.com/tmm/django-username-email/blob/master/cuser/admin.py
    """

    class Meta:
        verbose_name = _('group')
        verbose_name_plural = _('groups')
        proxy = True


admin.site.unregister(DjangoGroup)


@admin.register(Group)
class GroupAdmin(BaseGroupAdmin):
    pass
Enallage answered 10/12, 2021 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.