[Django]-How do I inline edit a django user profile in the admin interface?

18đź‘Ť

âś…

Well, it turns out that this is quite easy, once you know how to do it. This is my myapp/accounts/admin.py:

from django.contrib import admin
from myapp.accounts.models import UserProfile
from django.contrib.auth.models import User

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    max_num = 1
    can_delete = False

class AccountsUserAdmin(admin.UserAdmin):
    inlines = [UserProfileInline]

# unregister old user admin
admin.site.unregister(User)
# register new user admin that includes a UserProfile
admin.site.register(User, AccountsUserAdmin)

The default admin.UserAdmin ModelAdmin class for users is unregistered and a new one specifying an inline UserProfile is registered in its place. Just thought I should share.

34đź‘Ť

I propse a slightly improved version of André’s solution as it breaks the list view in /admin/auth/user/:

from django.contrib import admin
from member.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserProfileInline(admin.StackedInline):
 model = UserProfile
 max_num = 1
 can_delete = False

class UserAdmin(AuthUserAdmin):
 inlines = [UserProfileInline]

# unregister old user admin
admin.site.unregister(User)
# register new user admin
admin.site.register(User, UserAdmin)

17đź‘Ť

I propose another improvement to Robert’s solution:

from django.contrib import admin
from member.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserProfileInline(admin.StackedInline):
   model = UserProfile
   max_num = 1
   can_delete = False

class UserAdmin(AuthUserAdmin):
   def add_view(self, *args, **kwargs):
      self.inlines = []
      return super(UserAdmin, self).add_view(*args, **kwargs)

   def change_view(self, *args, **kwargs):
      self.inlines = [UserProfileInline]
      return super(UserAdmin, self).change_view(*args, **kwargs)

# unregister old user admin
admin.site.unregister(User)
# register new user admin
admin.site.register(User, UserAdmin)

Without this change to UserAdmin, the custom UserProfileInline section will show up on the “add user” screen, which is just supposed to ask for the username and password. And if you change any of the profile data on that screen (away from the defaults) before you save, you’ll get a “duplicate key” database error.

👤abjennings

0đź‘Ť

You have to consider the add and change form. OTherwise you will get a user cannot be None error when trying to create a user. The following has been tested and works in 1.3:

class TeamInline(admin.StackedInline):
  model = Team
  fk_name = 'user'
  max_num = 1
  can_delete = False

class TeamUserAdmin(UserAdmin):
  list_display = ('username', 'email', 'company', 'expertise', 'contact_email', 'contact_phone', 'twitter', 'facebook', 'last_login_short', 'options')
  list_select_related = True

  def add_view(self, *args, **kwargs):
    self.inline_instances = []
    return super(TeamUserAdmin, self).add_view(*args, **kwargs)

  def change_view(self, *args, **kwargs):
    self.inline_instances.append(TeamInline(self.model, self.admin_site))
    return super(TeamUserAdmin, self).change_view(*args, **kwargs)
👤Paul Kenjora

Leave a comment