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
?