| 653 | Adding UserProfile fields to the admin |
| 654 | -------------------------------------- |
| 655 | |
| 656 | To add the UserProfile fields to the user page in the admin, define an |
| 657 | :class:`~django.contrib.admin.InlineModelAdmin` (for this example, we'll use a |
| 658 | :class:`~django.contrib.admin.StackedInline`) in your app's ``admin.py`` and |
| 659 | add it to a ``UserAdmin`` class which is registered with the |
| 660 | :class:`~django.contrib.auth.models.User` class:: |
| 661 | |
| 662 | from django.contrib import admin |
| 663 | from django.contrib.auth.admin import UserAdmin |
| 664 | from django.contrib.auth.models import User |
| 665 | |
| 666 | from my_user_profile_app.models import UserProfile |
| 667 | |
| 668 | # Define an inline admin descriptor for UserProfile model |
| 669 | # which acts a bit like a singleton |
| 670 | class UserProfileInline(admin.StackedInline): |
| 671 | model = UserProfile |
| 672 | can_delete = False |
| 673 | verbose_name_plural = 'profile' |
| 674 | |
| 675 | # Define a new User admin |
| 676 | class UserAdmin(UserAdmin): |
| 677 | inlines = (UserProfileInline, ) |
| 678 | |
| 679 | # Re-register UserAdmin |
| 680 | admin.site.unregister(User) |
| 681 | admin.site.register(User, UserAdmin) |
| 682 | |