在管理员中组合User和UserProfile [英] Combining User and UserProfile in the admin

查看:147
本文介绍了在管理员中组合User和UserProfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Django分离的用户和个人资料,我决定使用一个名为UserProfile的模型,该模型位于帐户应用程序中作为我的个人资料。问题是,现在我有两个不同的管理区域,一个用于修改User,另一个用于修改User Profile。是否可以在管理员中查看两个模型,所以如果您添加或修改用户,您可以在同一视图中看到用户和配置文件的所有字段?也不用说,添加删除用户应该添加或删除配置文件,并且不能在没有用户的情况下添加或删除配置文件。



我已经看到了如何使这项工作(例如,添加用户时添加配置文件)的一些部分,但不是整体。

解决方案

您可以使用内联管理员模型



在编写您的自定义用户管理员您必须注销已经注册的用户 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆