Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#16929 closed New feature (fixed)

Document how to extend UserAdmin with extra profile information

Reported by: jasper@… Owned by: nobody
Component: Documentation Version: 1.3
Severity: Normal Keywords: auth, admin
Cc: timograham@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've been struggling to get this done, failing in the end and reverting to two separate admin sections; one for users and one for user profiles. It seems that the only way to properly include the UserProfile admin inside the User admin is by overwriting the standard User admin template and view. If that's really the case, I'd say it makes sense to, instead, automatically extend the User admin with the form that belongs to the AUTH_PROFILE_MODULE model, since there is a one to one relationship between the two anyway.

Attachments (1)

16929.diff (1.8 KB ) - added by Tim Graham 11 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by Simon Charette, 12 years ago

I think you can include the UserProfile admin inside the User admin doing the following:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from my_user_profile_app import UserProfile

# Define an inline admin descriptor for UserProfile model
# which acts a bit like a singleton
class UserProfileInline(admin.TabularInline):
    model = UserProfile
    fk_name = 'user'
    can_delete = False
    max_num = 1 
    verbose_name_plural = _('profile')

# Define a new User admin
class UserAdmin(UserAdmin):
    inlines = (UserProfileInline, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Last edited 12 years ago by Simon Charette (previous) (diff)

comment:2 by Julien Phalip, 12 years ago

Component: contrib.authDocumentation
Summary: The form for AUTH_PROFILE_MODULE should extend the basic User admin pageDocument how to extend UserAdmin with extra profile information
Triage Stage: UnreviewedAccepted

Automatically extending UserAdmin would be a bit too magical. charettes has given a great example for how to properly extend UserAdmin. This would be a nice addition to the documentation.

comment:3 by Matthew Schinckel, 12 years ago

One issue with this is that if you had two apps that both attempted to add something (like an inline) to the UserAdmin, only one of them (the last in INSTALLED_APPS?) would take.

in reply to:  3 comment:4 by Simon Charette, 12 years ago

Replying to schinckel:

One issue with this is that if you had two apps that both attempted to add something (like an inline) to the UserAdmin, only one of them (the last in INSTALLED_APPS?) would take.

This might feel a bit hackish but you can do the following:

#my_nth_app/admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from my_nth_app.models import MyNthAppModel

class OneOfMyNthAppInline(admin.TabularInline):
    model = MyNthAppModel
    fk_name = 'user'

#hackish
RegisteredUserAdmin = admin.site._registry.get(User)

class MyNthAppUserAdmin(RegisteredUserAdmin):
    inlines = RegisteredUserAdmin.inlines + [OneOfMyNthAppInline]

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, MyNthAppUserAdmin)

I know tickets aren't the right place to provide such examples but since it's the only way (I'm aware of) to achieve what @schinckel was trying to do without coupling all the apps I think it was worth posting until a proper solution can be documented.

comment:5 by Chris Wilson, 12 years ago

Please can we add this to the documentation?

A link to this ticket at https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users would have saved me half an hour searching.

by Tim Graham, 11 years ago

Attachment: 16929.diff added

comment:6 by Tim Graham, 11 years ago

Cc: timograham@… added
Has patch: set

comment:7 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 22242c510f84c53803afe2907649c892cb1b3d9a:

Fixed #16929 - Documented how to extend UserAdmin with UserProfile fields; thanks charettes for the draft example.

comment:8 by Tim Graham <timograham@…>, 11 years ago

In 18d88a169fecf7efa2fba2ba2867e7a37084c7fc:

[1.4.x] Fixed #16929 - Documented how to extend UserAdmin with UserProfile fields; thanks charettes for the draft example.

Backport of 22242c510f84c53803afe2907649c892cb1b3d9a from master.

Note: See TracTickets for help on using tickets.
Back to Top