Display first name and last name for user in dropdown lists in the Admin

1,342 views
Skip to first unread message

derek

unread,
Apr 22, 2010, 8:52:33 AM4/22/10
to Django users, game...@gmail.com
This thread:
http://groups.google.com/group/django-users/browse_thread/thread/b303c1ce3e724407/d99ee36e031d7393
ends with the comment that "whatever string (actually unicode)
representation you want as constructed from the object attributes when
you bring an object via foreign key."

This is certainly true, and the example pointed to shows this:

class Choice(models.Model):
# ...
def __unicode__(self):
return self.choice

However, what does the equivalent code look like for the customizing
the built-in admin user (actually stored as "auth_user" in the
database), including the neccesary class statement line?

(Detailed issue: I have been using UserProfile to customize the User
for my application. However, all the links to the user in the other
models go direct to User and not UserProfile. Ideally, I'd like the
dropdown list for users to show the information returned by this:

def __unicode__(self):
return u'%s,%s (%s)' % (self.last_name, self.first_name,
self.fieldX)

where "fieldX' is a field added in the UserProfile. If someone can
show me the code to achieve this, it would be incredibly helpful...
and maybe I could learn something about users and profiles!)

Thanks
Derek

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Tom Evans

unread,
Apr 22, 2010, 9:03:41 AM4/22/10
to django...@googlegroups.com
http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

Register a different ModelAdmin for the auth.User class, and define
the fields you wish to display. Note that you can pass a callable as a
field, and the callable will be called with the user instance, from
which you can generate whatever content you like, even from the
UserProfile.

Cheers

Tom

derek

unread,
Apr 22, 2010, 10:13:45 AM4/22/10
to Django users
On Apr 22, 3:03 pm, Tom Evans <tevans...@googlemail.com> wrote:
> On Thu, Apr 22, 2010 at 1:52 PM, derek <gamesb...@gmail.com> wrote:
> > This thread:
> >http://groups.google.com/group/django-users/browse_thread/thread/b303...
> > ends with the comment that "whatever string (actually unicode)
> > representation you want as constructed from the object attributes when
> > you bring an object via foreign key."
>
> > This is certainly true, and the example pointed to shows this:
>
> > class Choice(models.Model):
> >    # ...
> >    def __unicode__(self):
> >        return self.choice
>
> > However, what does the equivalent code look like for the customizing
> > the built-in admin user (actually stored as "auth_user" in the
> > database), including the neccesary class statement line?
>
> > (Detailed issue:  I have been using UserProfile to customize the User
> > for my application.  However, all the links to the user in the other
> > models go direct to User and not UserProfile.  Ideally, I'd like the
> > dropdown list for users to show the information returned by this:
>
> >    def __unicode__(self):
> >        return u'%s,%s (%s)' % (self.last_name, self.first_name,
> > self.fieldX)
>
> > where "fieldX' is a field added in the UserProfile.  If someone can
> > show me the code to achieve this, it would be incredibly helpful...
> > and maybe I could learn something about users and profiles!)
>
> > Thanks
> > Derek
>
> http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#django.contri...
>
> Register a different ModelAdmin for the auth.User class, and define
> the fields you wish to display. Note that you can pass a callable as a
> field, and the callable will be called with the user instance, from
> which you can generate whatever content you like, even from the
> UserProfile.
>
> Cheers
>
> Tom

Thanks Tom

I had thought I understood these concepts, but cannot see how to apply
them to the User specifically. That is why specific examples of code
always help (apart from the ones in the manual).

What would the code for "Register a different ModelAdmin for the
auth.User class" look like?

And can you expand further on this : "you can pass a callable as a
field, and the callable will be called with the user instance, from
which you can generate whatever content you like, even from the
UserProfile." How would this be represented in code form?

(I appreciate I may seem obtuse here, but for some reason I can't
grasp these aspects of Django although everything else about it has
made perfect sense up to now.)

Thanks
Derek

Tom Evans

unread,
Apr 22, 2010, 10:32:18 AM4/22/10
to django...@googlegroups.com
On Thu, Apr 22, 2010 at 3:13 PM, derek <game...@gmail.com> wrote:
> Thanks Tom
>
> I had thought I understood these concepts, but cannot see how to apply
> them to the User specifically.  That is why specific examples of code
> always help (apart from the ones in the manual).
>
> What would the code for "Register a different ModelAdmin for the
> auth.User class" look like?

Something like this:

from django.contrib.auth.models import User
admin.site.unregister(User)
admin.site.register(User, ReplacementUserAdmin)


>
> And can you expand further on this : "you can pass a callable as a
> field, and the callable will be called with the user instance, from
> which you can generate whatever content you like, even from the
> UserProfile."  How would this be represented in code form?
>

Like so:

def some_func(obj):
return u"%s %s" % (obj.email, obj.get_profile().telephone)

class ReplacementUserAdmin(admin.ModelAdmin):
list_display = (some_func, 'email')

Cheers

Tom

brad

unread,
Apr 22, 2010, 11:36:40 AM4/22/10
to Django users
This thread has prompted me to blog about my solution to this
problem. Here's how I've done this:

1) I created a UserModelChoiceField(ModelChoiceField) class whose
label_from_instance method returns a string containing the User's full
name (and username in parenthesis)
2) Create a ModelForm for the Model containing the FK to User. In this
form, use the UserModelChoiceField to override the FK attribute.
3) In the ModelAdmin class, set a form attribute that points to the
above ModelForm.

More detail (and some samples) are available here:
http://bradmontgomery.blogspot.com/2010/04/pretty-options-for-djangos-authuser.html

There's probably a simpler way, but I've been using this method for
about a year in some production code.
Reply all
Reply to author
Forward
0 new messages