问题
If you want to store extra information about a user (django.contrib.auth.models.User) in Django you can use the nifty AUTH_PROFILE_MODULE to plug in a "profile" model. Each user then gets a profile. It's all described here:
- http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
- http://www.djangobook.com/en/1.0/chapter12/#cn222
Now, let's say I have created an application called accounts with a model called UserProfile and registered it as the profile model for my users. How do I inline the editing of the profile in the admin interface for editing users (or vice versa)?
回答1:
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)
回答2:
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.
回答3:
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.
回答4:
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)
来源:https://stackoverflow.com/questions/3400641/how-do-i-inline-edit-a-django-user-profile-in-the-admin-interface