Opened 15 years ago

Closed 15 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: Paul McLanahan Owned by: Paul McLanahan
Component: contrib.auth Version: dev
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: no UI/UX: no

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 Paul McLanahan 15 years ago.
Patch

Download all attachments as: .zip

Change History (6)

by Paul McLanahan, 15 years ago

Patch

comment:1 by Paul McLanahan, 15 years ago

Owner: changed from nobody to Paul McLanahan
Status: newassigned

comment:2 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedDesign 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 by Paul McLanahan, 15 years ago

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 by Paul McLanahan, 15 years ago

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 by Malcolm Tredinnick, 15 years ago

Resolution: wontfix
Status: assignedclosed

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).

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