从ForeignKey字段获取用户

发布于 2024-09-30 06:25:31 字数 225 浏览 1 评论 0原文

有人可以告诉我从外键字段获取用户配置文件的最佳方法吗?

author = models.ForeignKey(User)

{{ object.author }} 只给我用户名,但我想要整个用户个人资料。

另外,有没有办法改变用户在管理字段中的显示方式?我希望您可以根据名字和名字来选择作者姓氏...目前列表仅列出用户的用户名。

谢谢!

Can someone please tell me the best way to get the users profile from a ForeignKey field?

author = models.ForeignKey(User)

{{ 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2024-10-07 06:25:31

使用 {{ object.author }} 实际上调用该对象上的 __unicode__ 方法(实际上显示用户名)。您必须创建自己的视图才能在单独的页面上显示用户个人资料,或者您可以使用 {{ object.author.first_name }} 等来显示所有字段。

最终,如果您还想在管理字段中添加其他内容,则必须覆盖默认的用户 __unicode__ 方法。

或者您可以查看此文档页面: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文