Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inline edit a django user profile in the admin interface?

Tags:

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

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)?

like image 592
André Laszlo Avatar asked Aug 03 '10 20:08

André Laszlo


People also ask

What is admin interface in Django?

Django provides a default admin interface which can be used to perform create, read, update and delete operations on the model directly. It reads set of data that explain and gives information about data from the model, to provide an instant interface where the user can adjust contents of the application .

How can I see profile in Django?

Open users app urls.py and add the route for profile view. Create the template for the view inside the users app template directory. We will modify this template later to display profile of the user, but first there are a couple of things we need to do.

What is staff status in Django admin?

staff. - A user marked as staff can access the Django admin. But permissions to create, read, update and delete data in the Django admin must be given explicitly to a user. By default, a superuser is marked as staff.


2 Answers

I propse a slightly improved version of André's solution as it breaks the list view in /admin/auth/user/:

from django.contrib import admin from member.models import UserProfile from django.contrib.auth.models import User from django.contrib.auth.admin import UserAdmin as AuthUserAdmin  class UserProfileInline(admin.StackedInline):  model = UserProfile  max_num = 1  can_delete = False  class UserAdmin(AuthUserAdmin):  inlines = [UserProfileInline]  # unregister old user admin admin.site.unregister(User) # register new user admin admin.site.register(User, UserAdmin) 
like image 181
Robert Lacroix Avatar answered Sep 19 '22 14:09

Robert Lacroix


I propose another improvement to Robert's solution:

from django.contrib import admin from member.models import UserProfile from django.contrib.auth.models import User from django.contrib.auth.admin import UserAdmin as AuthUserAdmin  class UserProfileInline(admin.StackedInline):    model = UserProfile    max_num = 1    can_delete = False  class UserAdmin(AuthUserAdmin):    def add_view(self, *args, **kwargs):       self.inlines = []       return super(UserAdmin, self).add_view(*args, **kwargs)     def change_view(self, *args, **kwargs):       self.inlines = [UserProfileInline]       return super(UserAdmin, self).change_view(*args, **kwargs)  # unregister old user admin admin.site.unregister(User) # register new user admin admin.site.register(User, UserAdmin) 

Without this change to UserAdmin, the custom UserProfileInline section will show up on the "add user" screen, which is just supposed to ask for the username and password. And if you change any of the profile data on that screen (away from the defaults) before you save, you'll get a "duplicate key" database error.

like image 35
abjennings Avatar answered Sep 18 '22 14:09

abjennings