在管理员中组合User和UserProfile [英] Combining User and UserProfile in the admin
问题描述
我已经看到了如何使这项工作(例如,添加用户时添加配置文件)的一些部分,但不是整体。
您可以使用内联管理员模型
在编写您的自定义用户
管理员您必须注销已经注册的用户
admin
admin.site.unregister(User)
定义Inline UserProfile
class UserProfileInline(admin.TabularInline):
model = UserProfile
,并使用用户
adm中的内联在
class UserAdmin(admin.ModelAdmin):
inlines = [UserProfileInline]
admin.site。注册(User,UserAdmin)
I have been reading up on Django's separation of Users and Profiles, and I have decided to go with a model called UserProfile that is located in an Accounts app as my Profile. The problem is, now I have two separate areas of the admin, one for modifying the User, and one for modifying the User Profile. Is it possible to view the two models as one in the admin, so if you add or modify a user you see all of the fields for both User and Profile in the same view? It also kinda goes without saying that adding a deleting a user should add or delete a profile with it, and it shouldn't be possible to add or delete a profile without the user.
I've seen bits and pieces of how to make this work (for example, something that adds a profile when you add a user), but not as a whole.
You can do this by using inline admin models
before writing your custom User
admin you have to unregister the already registered User
admin
admin.site.unregister(User)
define the Inline UserProfile
class UserProfileInline(admin.TabularInline):
model = UserProfile
and use the inline in the User
admin
class UserAdmin(admin.ModelAdmin):
inlines = [UserProfileInline]
admin.site.register(User, UserAdmin)
这篇关于在管理员中组合User和UserProfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!