djangodjango-admin

Hide first_name and last_name in Django's admin


I don't like coercing a user's name into separate first_name and last_name fields, so I replaced user.first_name and user.last_name with profile.fullname.

It doesn't make sense to show all three names in the admin, so I tried this:

admin.site.unregister(User)

class ProfileInline(admin.StackedInline):
    model = Profile

class ProfileAdmin(UserAdmin):
    inlines = [ProfileInline]
    exclude = ('first_name', 'last_name')

admin.site.register(User, ProfileAdmin)

That doesn't work, and I can't tell why:

Caught KeyError while rendering: "Key 'first_name' not found in Form"


Solution

  • Refs the code, the 'last_name' and 'first_name' have already been declared in fieldsets. Then the error occurs because the layout declaration says there are last_name and first_name which can no longer be found in form fields. Override fieldsets will do the trick.