开发者

Combining User and UserProfile in the admin

开发者 https://www.devze.com 2023-01-14 17:26 出处:网络
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 ha

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 e开发者_如何学编程xample, 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消