views:

33

answers:

1

I attached a UserProfile class to User this way:

class UserProfile(models.Model):
    url = models.URLField()
    home_address = models.TextField()
    user = models.ForeignKey(User, unique=True)

I have also implemented auto-creating of UserProfile if needed this way:

def user_post_save(sender, instance, signal, *args, **kwargs):
    profile, new = UserProfile.objects.get_or_create(user=instance)

models.signals.post_save.connect(user_post_save, sender=User)

It works fine but I need a small feature - when I go to User Profiles in admin console, I see a list of UserProfiles for existing users. Their titles are shown as UserProfile object. I think it would be nice if I could set titles to corresponding user names, for example, john, kenny etc. How can I do that?

+4  A: 

Define a __unicode__ method for the UserProfile class:

def __unicode__(self):
   return u"Profile for %s" % self.user.get_full_name() 
stevejalim
or self.user.username if you prefer
stevejalim
+1. This is the way to do it.
Manoj Govindan
Works just perfect. Thanks!
Sergey Basharov
+1, mmm, yummy unicode...
rebus