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.
The solution.
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
- admin.site.unregister(User)
- 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.






Comments
May 1, 2009
9:38 am
June 1, 2009
5:26 pm
June 4, 2009
4:22 pm
June 13, 2009
2:50 am
July 10, 2009
4:33 am
September 14, 2009
7:23 am
October 15, 2009
8:33 am
Whaddya think?