[Django]-Django Administration: Delete record from custom user model

0👍

I have a similar abstracted user model in a django application I am working on. This is how I have my admin.py setup and it works per your request.

I have modified your admin.py to look more like mine. I believe when you are using the ‘get_user_model’ function, it is causing the problem.

Also, unless you have a specific reason to add the fields email, username, is_active, staff. Subclassing the abstract user model already provides all of the default fields. I have added an additional field to foreign key a user to a profile type model. I use the UserAdmin model and field sets to display my custom user field in the django admin.

from django.contrib import admin

# Import your abstract user model from the apps models.py
from .models import Profile, User

# Import the auth UserAdmin model
from django.contrib.auth.admin import UserAdmin


# I use this to add the custom fields the django admin form, you may not need it. 
fields = list(UserAdmin.fieldsets)
fields[0] = (None, {'fields': ('username','password','account')})
UserAdmin.fieldsets = tuple(fields)

admin.site.register(Profile)
# Register the two models
admin.site.register(User, UserAdmin) 

For reference, here is what my models.py looks like for the user model.

class User(AbstractUser):
account = models.ForeignKey('Account', on_delete=models.PROTECT, null=True, blank=True, unique=True)

class Meta:
    permissions = (
                   ("make_admin", "Can view and edit most admin features."),
                   ("edit_permissions", "Admin user can modify user permissions."),
                   ("edit_nacha", "User can edit and modify NACHA files."),
                   ("edit_commissions", "User can override commisions."),
                   ("view_reports", "User can view admin reports."),
                  )
👤noes1s

-1👍

You’ll need to update your settings.py and point to the custom user model

settings.py

# Custom User model
AUTH_USER_MODEL = 'my_app.User'

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from my_app.models import User

admin.site.register(User, UserAdmin)

See: https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#substituting-a-custom-user-model

Leave a comment