Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining User and UserProfile in the admin

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.

like image 284
AlexMax Avatar asked Aug 31 '10 01:08

AlexMax


1 Answers

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)
like image 143
Ashok Avatar answered Oct 06 '22 01:10

Ashok