Code

Opened 5 years ago

Closed 5 years ago

#10858 closed (wontfix)

More useful output for User.__unicode__ allowing easy formatting of all admin forms that list users from settings

Reported by: pmclanahan Owned by: pmclanahan
Component: contrib.auth Version: master
Severity: Keywords: auth user model unicode
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When choosing a user in an admin form for a model with a relationship to the django.auth.models.User model, the field always shows the username attribute, which in many systems isn't very informative. This patch would allow control over the formatting of those fields from the settings module.

This introduces a new setting named "AUTH_USER_STR_FORMATTER", which should be set to a callable that will be passed the User model instance and is expected to return a string to be used as a plain text representation of the User object.

Example:

# settings.py

def user_str_formatter(user):
    fname = user.get_full_name()
    return "%s (%s)" % (fname, user.username) if fname else user.username

AUTH_USER_STR_FORMATTER = user_str_formatter

Attachments (1)

auth_user_model_unicode.diff (1.2 KB) - added by pmclanahan 5 years ago.
Patch

Download all attachments as: .zip

Change History (6)

Changed 5 years ago by pmclanahan

Patch

comment:1 Changed 5 years ago by pmclanahan

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to pmclanahan
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Design decision needed

This feels like adding a setting to control something you can already do. Either use proxy subclasses to overide unicode, or just output what you want where you what. For those reasons it is my strong inclination to wontfix this, however I'm going to mark it as DDN.

comment:3 Changed 5 years ago by pmclanahan

I did try a couple things with which I wasn't fully satisfied. My main thought was that the User model is used in relationships so often that a more central approach to modifying the model class itself might adhere more closely to DRY. I'm very open to the idea that I just haven't found the best other solution though. Thanks for the comments.

comment:4 Changed 5 years ago by pmclanahan

You're completely right Alex. Using my own proxy model would be best. I just spaced on that for some reason. I do think that this particular case is so common that it might warrant a setting and not require something that I would consider to be a more advanced technique, but I do see your point. Thanks again.

comment:5 Changed 5 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from assigned to closed

Not worth it. Firstly, there's proxy models now. More importantly, it's symptom patching. The only case this is really a problem (where you are forced to use the __unicode__ method) is when displaying related models in the admin. That's not at all specific to the User model and should be solved holistically and probably entirely within the Admin interface (or, at a minimum, within the related fields infrastructure).

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.