Code

Opened 3 years ago

Closed 22 months ago

Last modified 22 months 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 timo 22 months ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 3 years ago by charettes

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 3 years ago by charettes (previous) (diff)

comment:2 Changed 3 years ago by julien

  • Component changed from contrib.auth to Documentation
  • Summary changed from The form for AUTH_PROFILE_MODULE should extend the basic User admin page to Document how to extend UserAdmin with extra profile information
  • Triage Stage changed from Unreviewed to Accepted

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 follow-up: Changed 3 years ago by 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.

comment:4 in reply to: ↑ 3 Changed 3 years ago by charettes

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 Changed 2 years ago by gcc

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.

Changed 22 months ago by timo

comment:6 Changed 22 months ago by timo

  • Cc timograham@… added
  • Has patch set

comment:7 Changed 22 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 22242c510f84c53803afe2907649c892cb1b3d9a:

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

comment:8 Changed 22 months ago by Tim Graham <timograham@…>

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.