如何在管理界面中内联编辑django用户配置文件? [英] How do I inline edit a django user profile in the admin interface?
问题描述
如果您想在Django中存储关于用户的额外信息(django.contrib.auth.models.User),您可以使用漂亮的AUTH_PROFILE_MODULE插入配置文件模型。然后每个用户都获得一个配置文件。这一切都在这里描述:
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
- http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
- http://www.djangobook.com/en/1.0/chapter12/#cn222
现在,我创建了一个名为accounts的应用程序,名为UserProfile并将其注册为我的用户的配置文件模型。 如何在管理界面中编辑个人资料以进行编辑(或反之亦然)?
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)?
推荐答案
那么事实证明,一旦你知道如何做,这很简单。这是我的myapp / accounts / admin.py:
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)
默认的 admin.UserAdmin
用户的ModelAdmin类未注册,并指定一个新的内容 UserProfile
已注册在其位置。只是想我应该分享。
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.
这篇关于如何在管理界面中内联编辑django用户配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!