Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Don't assume USERNAME_FIELD == 'user' in UserAdmin. #427

Closed
wants to merge 1 commit into from

2 participants

Gavin Wahl Preston Holmes
Gavin Wahl

Never assume that a user object has a field named username, use
USERNAME_FIELD instead.

fixes https://code.djangoproject.com/ticket/19077

Gavin Wahl gavinwahl Don't assume USERNAME_FIELD == 'user' in UserAdmin.
Never assume that a user object has a field named `username`, use
`USERNAME_FIELD` instead.

fixes #19077
af667c2
Preston Holmes
Collaborator

as clarified in https://code.djangoproject.com/ticket/19077#comment:6 - the default ModelAdmin will not easily work with custom user models that don't largely conform to auth.User, and the solution is likely to subclass UserAdmin

Preston Holmes ptone closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 5, 2012
  1. Gavin Wahl

    Don't assume USERNAME_FIELD == 'user' in UserAdmin.

    gavinwahl authored
    Never assume that a user object has a field named `username`, use
    `USERNAME_FIELD` instead.
    
    fixes #19077
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 2 deletions.
  1. +3 −2 django/contrib/auth/admin.py
5 django/contrib/auth/admin.py
View
@@ -106,9 +106,10 @@ def add_view(self, request, form_url='', extra_context=None):
raise PermissionDenied
if extra_context is None:
extra_context = {}
+ username_field = self.model._meta.get_field(getattr(self.model, 'USERNAME_FIELD', 'username'))
defaults = {
'auto_populated_fields': (),
- 'username_help_text': self.model._meta.get_field('username').help_text,
+ 'username_help_text': username_field.help_text,
}
extra_context.update(defaults)
return super(UserAdmin, self).add_view(request, form_url,
@@ -133,7 +134,7 @@ def user_change_password(self, request, id, form_url=''):
adminForm = admin.helpers.AdminForm(form, fieldsets, {})
context = {
- 'title': _('Change password: %s') % escape(user.username),
+ 'title': _('Change password: %s') % escape(str(user)),
'adminForm': adminForm,
'form_url': form_url,
'form': form,
Something went wrong with that request. Please try again.