The built in admin pages that you get in Django can be useful, but they particularly become useful once you start to add a lot more functionality to them.
For instance the Django’s User authentication system (which lives in django.contrib.auth ) is widely used, and quite often you need to extend the user’s profile by using AUTH_PROFILE_MODULE and a separate model. But having a separate Admin screen for this is kind of pointless.
In the admin.py file for your model which provides extra information (in my example it is called UserProfile just do the following:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from models import UserProfile
class UserProfileInline(admin.TabularInline):
model = UserProfile
fk_name = 'user'
max_num = 1
class CustomUserAdmin(UserAdmin):
inlines = [UserProfileInline,]
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active')
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from models import UserProfile
class UserProfileInline(admin.TabularInline):
model = UserProfile
fk_name = 'user'
max_num = 1
class CustomUserAdmin(UserAdmin):
inlines = [UserProfileInline,]
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active')
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
Simple once you know how.
4 Comments
I wish shit like this was better documented…either that or I just need more experience working with the django admin interface. Inlining the UserProfile should have been obvious but I would never have thought to unregister the User and reregister.
There are so many cool things that you can do with django that are just under documented.
Lots of cool stuff that you can do for sure. But they have done quite an amazing job of the documentation anyways.
Hey,
thanks for the tutorial, but I am still not able to display my attributes with pre-inserted attributes. How can i do that ? any idea ?
Not really sure what you mean? you can display the attributes by User.get_profile().attributename