问题描述
如果您想在 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
现在,假设我使用名为 UserProfile 的模型创建了一个名为 accounts 的应用程序,并将其注册为我的用户的配置文件模型.如何在管理界面内联编辑个人资料以编辑用户(反之亦然)?
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 用户配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!