在子管理中显示父字段(list_display)

发布于 2024-10-15 23:48:15 字数 895 浏览 2 评论 0原文

这是 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 技术交流群。

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

发布评论

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

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-10-22 23:48:15

您可以像 list_display 文档。只需向您的应用程序模型添加一个方法,如下所示:

class Application(models.Model):
    applicant = models.ForeignKey(Applicant)
    text = models.TextField(...)

    def applicant_name(self):
        return self.applicant.name
    applicant_name.short_description = 'Applicant Name'

    def applicant_email(self):
        return self.applicant.email
    applicant_email.short_description = 'Applicant Email'

然后您可以像这样设置 ModelAdmin:

class ApplicationAdmin(model.ModelAdmin):
    list_display = ['text', 'applicant_name', 'applicant_email']

You can do it like the fourth possibility in the list_display docs. Just add a method to your Application model like so:

class Application(models.Model):
    applicant = models.ForeignKey(Applicant)
    text = models.TextField(...)

    def applicant_name(self):
        return self.applicant.name
    applicant_name.short_description = 'Applicant Name'

    def applicant_email(self):
        return self.applicant.email
    applicant_email.short_description = 'Applicant Email'

And then you can setup your ModelAdmin like so:

class ApplicationAdmin(model.ModelAdmin):
    list_display = ['text', 'applicant_name', 'applicant_email']
开始看清了 2024-10-22 23:48:15

如果您只需要在列表中显示:

class Applicant(models.Model):
    name = models.CharField(...)
    email = models.CharField(...)

    def __unicode__(self):
        return "%s <%s>" % (self.name, self.email)

class ApplicationAdmin(model.ModelAdmin):
    list_display = ['text', 'applicant']

回答您的评论

但希望将每个字段作为管理中的新行/行,而不是字符串。

不确定这是否是最好的解决方案,但它会起作用:

class Application(models.Model):

    @property
    def applicant__name(self):
        return self.applicant.name

    # or maybe:

    def __getattr__(self, name):
        if name.startswith('applicant__'):
            return gettattr(self.applicant, 
                            name[len('applicant__'):])
        raise AttributeError()

class ApplicationAdmin(model.ModelAdmin):
    list_display = ['text', 'applicant__name']

If you only need to display it in list:

class Applicant(models.Model):
    name = models.CharField(...)
    email = models.CharField(...)

    def __unicode__(self):
        return "%s <%s>" % (self.name, self.email)

class ApplicationAdmin(model.ModelAdmin):
    list_display = ['text', 'applicant']

Answer to your comment

but would like to have each field as a new line/row in the admin, not as a string.

Not sure if it is the best solution availabe but it will work:

class Application(models.Model):

    @property
    def applicant__name(self):
        return self.applicant.name

    # or maybe:

    def __getattr__(self, name):
        if name.startswith('applicant__'):
            return gettattr(self.applicant, 
                            name[len('applicant__'):])
        raise AttributeError()

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