1. Extending Django’s user admin

    The built in admin pages that you get in Django can be useful, but they particularly become useful once you start to add a lot more functionality to them.

    For instance the Django’s User authentication system (which lives in django.contrib.auth ) is widely used, and quite often you need to extend the user’s profile by using AUTH_PROFILE_MODULE and a separate model. But having a separate Admin screen for this is kind of pointless.

    In the admin.py file for your model which provides extra information (in my example it is called UserProfile just do the following:

    from django.contrib import admin
    from django.contrib.auth.admin import UserAdmin
    from django.contrib.auth.models import User
    from models import UserProfile

    class UserProfileInline(admin.TabularInline):
        model = UserProfile
        fk_name = 'user'
        max_num = 1
       
    class CustomUserAdmin(UserAdmin):
        inlines = [UserProfileInline,]
        list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active')
       
    admin.site.unregister(User)
    admin.site.register(User, CustomUserAdmin)

    Simple once you know how.

    By timc3 on the
    February 18th, 2010

11 Comments

  1. I wish shit like this was better documented…either that or I just need more experience working with the django admin interface. Inlining the UserProfile should have been obvious but I would never have thought to unregister the User and reregister.

    There are so many cool things that you can do with django that are just under documented.

    John Says:
    March 21st, 2010 at 1:17 am
  2. Lots of cool stuff that you can do for sure. But they have done quite an amazing job of the documentation anyways.

    timc3 Says:
    March 21st, 2010 at 1:59 am
  3. Hey,
    thanks for the tutorial, but I am still not able to display my attributes with pre-inserted attributes. How can i do that ? any idea ?

    dusean Says:
    June 18th, 2010 at 12:16 am
  4. Not really sure what you mean? you can display the attributes by User.get_profile().attributename

    timc3 Says:
    August 6th, 2010 at 12:57 pm
  5. Great post. Very elegant.

    Gavan Says:
    November 30th, 2010 at 9:58 pm
  6. Just wanted to say you thanks for this snippet. I was going crazy :D

    Javi Says:
    May 3rd, 2011 at 8:11 pm
  7. That’s a very nice and simple solution. Thank you!

  8. Thank you for the concise explanation, it definitely got me up and going on a ‘phase 1′ of editing my user via admin. However, StackedInline seems to expect a one to many type relationship, where one user can have many profiles. My conclusion is based on the fact that stacked inlines are appended to the bottom of the user form (in a collapsible form, if using grappelli) and I can’t seem to integrate the ProfileUser fields nicely with the user form. It would be nice to group the ProfileUser forms fields in with the User form fields (Personal Info, Business Info) etc, but Fieldsets seem to have no knowledge of inlines.

    Any suggestions?

    Imposter Says:
    December 8th, 2011 at 7:39 pm
  9. imposter: How are the models setup? Do you have a OneToOne relationship with the models, or OneToMany?

    I would suggest building more of a custom form where you include the others, but that really does mean more work.

    timc3 Says:
    December 9th, 2011 at 9:23 am
  10. SDC: Yes I haven’t tried this with Django 1.3, at the time it was written I think that I was on Django 1.2

    timc3 Says:
    December 9th, 2011 at 9:25 am

Please post a comment