Get user from ForeignKey field
Can someone please tell me the best way to get the users profile from a ForeignKey field?
author = models.ForeignKey(User)
{{开发者_StackOverflow社区 object.author }} just gives me the username but I want the entire users profile.
Also, is there a way to change the way the user is displayed in the admin field? I'd like it so you can chose the author based on their First Name & Last Name... at the moment the list just lists the users username.
Thanks!
Using {{ object.author }}
actually calls the __unicode__
method on that object (which actually displays username). You will have to create your own view to display the user profile on a separate page or you can use {{ object.author.first_name }}
etc to display all fields.
Eventually you will have to override the default user __unicode__
method if you want something else in the admin fields as well.
Or you can see this documentation page: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display
精彩评论