简体   繁体   English

Django如何在管理控制台中的个人资料中显示用户名

[英]Django How to show user's name in his profile in admin console

I attached a UserProfile class to User this way: 我通过以下方式将UserProfile类附加到User:

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: 如果需要,我还通过以下方式实现了UserProfile的自动创建:

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 . 它们的标题显示为UserProfile对象 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? 我认为将标题设置为相应的用户名(例如johnkenny等)会很好。我该怎么做?

Define a __unicode__ method for the UserProfile class: 为UserProfile类定义__unicode__方法:

def __unicode__(self):
   return u"Profile for %s" % self.user.get_full_name() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM