Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Django User model __unicode__

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

like image 286
Daryl Avatar asked Feb 21 '11 05:02

Daryl


1 Answers

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)
like image 177
Franz Avatar answered Oct 05 '22 15:10

Franz