Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - User full name as unicode

I have many Models linked to User and I'd like my templates to always display his full_name if available. Is there a way to change the default User __unicode__() ? Or is there another way to do it ?

I have a profile model registered where I can define the __unicode__(), should I link all my models to it ? Seems not a good idea to me.


Imagine I need to display the form for this object

class UserBagde
    user = model.ForeignKey(User)
    badge = models.ForeignKey(Bagde)

I will have to select box with __unicodes__ of each object, won't I ?
How can I have full names in the user's one ?

like image 588
Pierre de LESPINAY Avatar asked Aug 10 '12 13:08

Pierre de LESPINAY


People also ask

How do I make my Django username not unique?

If you want to use django's default authentication backend you cannot make username non unique. You will have to implement a class with get_user(user_id) and authenticate(request, **credentials) methods for a custom backend.

What is the use of Unicode in Django?

If you define a __unicode__() method, Django will call it when it needs to render an object in a context where a string representation is needed (e.g. in the model's admin pages). The documentation says: The __unicode__() method is called whenever you call unicode() on an object.

Is username unique in Django user model?

The default User model in Django uses a username to uniquely identify a user during authentication. If you'd rather use an email address, you'll need to create a custom User model by either subclassing AbstractUser or AbstractBaseUser .

Does Django automatically encode user input?

All of Django's database backends automatically convert strings into the appropriate encoding for talking to the database. They also automatically convert strings retrieved from the database into strings. You don't even need to tell Django what encoding your database uses: that is handled transparently.


3 Answers

Try this:

User.full_name = property(lambda u: u"%s %s" % (u.first_name, u.last_name))

EDIT

Apparently what you want already exists..

https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.get_full_name

ALSO

if its imperative that the unicode function be replaced:

def user_new_unicode(self):
    return self.get_full_name()

# Replace the __unicode__ method in the User class with out new implementation
User.__unicode__ = user_new_unicode 

# or maybe even
User.__unicode__ = User.get_full_name()

Fallback if name fields are empty

def user_new_unicode(self):
    return self.username if self.get_full_name() == "" else self.get_full_name()

# Replace the __unicode__ method in the User class with out new implementation
User.__unicode__ = user_new_unicode 
like image 129
Francis Yaconiello Avatar answered Oct 23 '22 04:10

Francis Yaconiello


If you have a profile model set up as Django suggests, you could define the full name on that model

from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    ...

@property
def full_name(self):
    return "%s %s" % (self.user.first_name, self.user.last_name)

then anywhere you have access to the user object you can easily do user.get_profile.full_name

Alternatively, if you only need the full name in the template you could write a simple tag:

@register.simple_tag
def fullname(user):
    return "%s %s" % (user.first_name, user.last_name)
like image 39
Timmy O'Mahony Avatar answered Oct 23 '22 05:10

Timmy O'Mahony


Just slam get_full_name to the __unicode__ method like so

User.__unicode__ = User.get_full_name

Make sure you override it with the callable, not the result of the function. User.get_full_name() will fail with the open and close brackets.

Place on any included file and you should be good.

like image 37
Jangita Avatar answered Oct 23 '22 03:10

Jangita