After digging around the Django source code, I found a working soul. I'm not quite happy with this solution, but it seems to work. Feel free to suggest the best solutions!
Django uses UserAdmin to visualize nice admin behavior for the User model. Just using this in our admin.py -file, we can get the same approach to our model.
from django.contrib.auth.admin import UserAdmin admin.site.register(MyUser, UserAdmin)
However, this in itself is probably not a good solution, since Django Admin will not display any of your custom fields. There are two reasons for this:
UserAdmin uses UserChangeForm as the form that will be used to modify the object, which in turn uses User as its model.UserAdmin defines formsets -property, later used by UserChangeForm , which does not include your custom fields.
So, I created a special change form that overloads the Meta inner class so that the change form uses the correct model. I also had to overload UserAdmin to add my custom fields to the field set that is part of this solution. I don't like the beat as it looks a little ugly. Feel free to suggest improvements!
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta(UserChangeForm.Meta): model = MyUser class MyUserAdmin(UserAdmin): form = MyUserChangeForm fieldsets = UserAdmin.fieldsets + ( (None, {'fields': ('some_extra_data',)}), ) admin.site.register(MyUser, MyUserAdmin)
nip3o Feb 21 '13 at 22:55 2013-02-21 22:55
source share