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:
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)?
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 .
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With