在子管理中显示父字段(list_display)
这是 models.py 的片段
class Applicant(models.Model):
name = models.CharField(...)
email = models.CharField(...)
class Application(models.Model):
applicant = models.ForeignKey(Applicant)
text = models.TextField(...)
这是我的 admin.py:
class ApplicationAdmin(model.ModelAdmin):
list_display = ['text', *******]
admin.site.register(Application, ApplicationAdmin)
在 ApplicationAdmin 中,我想显示申请人的姓名和电子邮件。
在询问之前您尝试过什么?
我查看了以下代码,该代码不起作用:
list_display = ['text', 'applicant__name','applicant__email']
我查看了ModelAdmin.inlines 但正如我们所见,父/子关系必须颠倒过来。
有什么建议吗?如何在应用程序管理中显示申请人姓名/电子邮件。最好不要使用新字段等迁移数据库。
Here is a snippet of models.py
class Applicant(models.Model):
name = models.CharField(...)
email = models.CharField(...)
class Application(models.Model):
applicant = models.ForeignKey(Applicant)
text = models.TextField(...)
Here is my admin.py:
class ApplicationAdmin(model.ModelAdmin):
list_display = ['text', *******]
admin.site.register(Application, ApplicationAdmin)
In the ApplicationAdmin I want to present the Applicants name and email.
What have you tried before asking SO?
I have looked at the following code, which does not work:
list_display = ['text', 'applicant__name','applicant__email']
I have looked at ModelAdmin.inlines but as one can see, the parent/child relationship have to be reversed.
Any suggestions? How can I display an applicants name/email in Applications admin. Prefferably without migrating the database with new fields etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像 list_display 文档。只需向您的应用程序模型添加一个方法,如下所示:
然后您可以像这样设置 ModelAdmin:
You can do it like the fourth possibility in the list_display docs. Just add a method to your Application model like so:
And then you can setup your ModelAdmin like so:
如果您只需要在列表中显示:
回答您的评论
不确定这是否是最好的解决方案,但它会起作用:
If you only need to display it in list:
Answer to your comment
Not sure if it is the best solution availabe but it will work: