After following James Bennett's 2006 article on extending Django's User model, I was surprised to find that the resulting profile fields didn't automagically appear in the admin panel. I knew it had to be possible. The solution just took some sleuthing.
A very brief explanation.
For those of you who haven't toyed around much with Django, all we're talking about is the ability to take the framework's built-in User system – which has all sorts of authentication shortcuts and permissions goodies baked in – and add an arbitrary number of extra fields. Profile information, if you will. Super handy stuff.
It turns out the solution was hiding inside a still unanswered question posited on the Google Groups message boards.
Assuming an app called "users" with an auth profile module called UserProfile (as in James Bennett's tutorial linked above), your admin.py for the users app would look like this:
- from django.contrib import admin
- from django.contrib.auth.models import User
- from django.contrib.auth.admin import UserAdmin
- from website.users.models import UserProfile
- class UserProfileInline(admin.StackedInline):
- model = UserProfile
- class UserProfileAdmin(UserAdmin):
- inlines = [UserProfileInline]
- admin.site.register(User, UserProfileAdmin)
For me, the big news is in that sixth line. The
unregister method on the site object is what lets us override the default behavior of the admin panel for built-in apps. I can see this being of use with other bundled apps – think flatpages or comments.