17

Currently, Django 1.2.3 User model unicode is

def __unicode__(self):
    return self.username

and I'd like to override it so its:

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

How to?

To similar effect:

User._meta.ordering = ['last_name', 'first_name']

works when defined anywhere

1

5 Answers 5

36

If you simply want to show the full name in the admin interface (which is what I needed), you can easily monkey-patch it during runtime. Just do something like this in your admin.py:

from django.contrib import admin
from django.contrib.auth.models import User

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

User.__unicode__ = user_unicode

admin.site.unregister(User)
admin.site.register(User)
2
  • 2
    You can do this anywhere in your code, not just for the admin site.
    – Pykler
    Jun 14, 2012 at 16:29
  • 1
    If you like one liners this works too: User.__unicode__ = lambda self: u'%s, %s' % (self.last_name, self.first_name)
    – ostergaard
    Sep 17, 2015 at 4:10
5

Django's Proxy Model solved this problem.

This is my solution:

form.fields['students'].queryset = Student.objects.filter(id__in = school.students.all())

Here school.students is a m2m(User), Student is a proxy model of User.

class Student(User):
    class Meta:
        proxy = True
    def __unicode__(self):
        return 'what ever you want to return'

All above helps you to solve if your want to show your User ForeignKey in your custom method. If your just want to change it in admin view, there is a simple solution:

def my_unicode(self):
    return 'what ever you want to return'

User.__unicode__ = my_unicode

admin.site.unregister(User)
admin.site.register(User)

add these codes to admin.py, it works.

0

If you need to override these, chances are you would need more customizations later on. The cleanest practice would be using a user profile models instead of touching the User model

4
  • So, does that mean relating to 'Profile', instead of 'User', i.e. author = models.ForeignKey(Profile) instead of author = models.ForeignKey(User) ??
    – Daryl
    Feb 21, 2011 at 23:32
  • 4
    I do understand the philosophy of the Profile model, yet, there's often a need to slightly modify the User output as well
    – Daryl
    Feb 21, 2011 at 23:33
  • You typically use "User" for stuff related to authentication and authorization, but use the "profile" model for your specific application needs. Django makes it easy to get the "Profile" model from the "User" model and vise versa (e.g. request.user.profile) (
    – GabiMe
    Feb 22, 2011 at 7:52
  • 1
    Note that approach descibed by the piece of documentation linked above is actually not recommended by core developers and is likely to become deprecated. A user profile model with a OneToOneField to User is OK, but you're better not to rely on AUTH_PROFILE_MODULE and get_profile(). Just use the normal ORM methods to access it (like user.profile.name, not user.get_profile().name). Some details here: convore.com/django-community/customizing-your-user-model/159304 Jun 9, 2011 at 8:18
0

Create a proxy User class.

class UserProxy(User):
    class Meta:
        proxy = True
        ordering = ['last_name', 'first_name']

    def __unicode__(self):
        return u'%s, %s' % (self.last_name, self.first_name)
1
  • 2
    This is prone to horrible bugs if you are planning to deal with permissions. I'd discourage anyone from using proxy models. Mar 3, 2012 at 10:35
0

I just found this simple method on django 1.5

def __unicode__(self):
   a = self.last_name
   b = self.first_name 
   c = a+ "-" +b
   return c 

it will return what you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.