Using Django auth UserAdmin for a custom user model Using Django auth UserAdmin for a custom user model django django

Using Django auth UserAdmin for a custom user model


After digging around the Django source code for a while, I found a working soultion. I am not totally happy with this solution, but it seems to work. Feel free to suggest better solutions!


Django uses UserAdmin to render the nice admin look for User model. By just using this in our admin.py-file, we can get the same look for our model.

from django.contrib.auth.admin import UserAdminadmin.site.register(MyUser, UserAdmin)

However, this alone is probably not a good solution, since Django Admin will not display any of your special fields. There are two reasons for this:

  • UserAdmin uses UserChangeForm as the form to be used when modifying the object, which in its turn uses User as its model.
  • UserAdmin defines a formsets-property, later used by UserChangeForm, which does not include your special fields.

So, I created a special change-form which overloads the Meta inner-class so that the change form uses the correct model. I also had to overload UserAdmin to add my special fields to the fieldset, which is the part of this solution I dislike a bit, since it looks a bit ugly. Feel free to suggest improvements!

from django.contrib.auth.admin import UserAdminfrom django.contrib.auth.forms import UserChangeFormclass MyUserChangeForm(UserChangeForm):    class Meta(UserChangeForm.Meta):        model = MyUserclass MyUserAdmin(UserAdmin):    form = MyUserChangeForm    fieldsets = UserAdmin.fieldsets + (            (None, {'fields': ('some_extra_data',)}),    )admin.site.register(MyUser, MyUserAdmin)


nico's answer has been extremely helpful but I found Django still references the User model when creating a new user.

Ticket #19353 references this problem.

In order to fix it i had to make a few more additions to admin.py

admin.py:

from django.contrib import adminfrom django.contrib.auth.admin import UserAdminfrom django.contrib.auth.forms import UserChangeForm, UserCreationFormfrom main.models import MyUserfrom django import formsclass MyUserChangeForm(UserChangeForm):    class Meta(UserChangeForm.Meta):        model = MyUserclass MyUserCreationForm(UserCreationForm):    class Meta(UserCreationForm.Meta):        model = MyUser    def clean_username(self):        username = self.cleaned_data['username']        try:            MyUser.objects.get(username=username)        except MyUser.DoesNotExist:            return username        raise forms.ValidationError(self.error_messages['duplicate_username'])class MyUserAdmin(UserAdmin):    form = MyUserChangeForm    add_form = MyUserCreationForm    fieldsets = UserAdmin.fieldsets + (        (None, {'fields': ('extra_field1', 'extra_field2',)}),    )admin.site.register(MyUser, MyUserAdmin)


A simpler solution, admin.py:

from django.contrib.auth.admin import UserAdminfrom main.models import MyUserclass MyUserAdmin(UserAdmin):    model = MyUser    fieldsets = UserAdmin.fieldsets + (            (None, {'fields': ('some_extra_data',)}),    )admin.site.register(MyUser, MyUserAdmin)

Django will correctly reference MyUser model for creation and modification.I'm using Django 1.6.2.