在Django管理员中用自定义详细名称覆盖list_display

11 投票
1 回答
10509 浏览
提问于 2025-04-17 17:36

我已经重写了列表显示,让它可以显示一些内联字段,代码是这样的:

class opportunityAdmin(admin.ModelAdmin):
    list_display = ('name', 'Contact', 'Phone', 'Address', 'discovery_date', 'status' , 'outcome')
    search_fields = ['name', 'tags' , 'description']
    #readonly_fields = ('discovery_date','close_date')
    inlines = [account_contactInline, account_updateInline]

    def Contact(self, obj):
        return '<br/>'.join(c.account_poc for c in account_contact.objects.filter(opportunity=obj.id).order_by('id')[:1])

    def Phone(self, obj):
        return '<br/>'.join(c.account_phone for c in account_contact.objects.filter(opportunity=obj.id).order_by('id')[:1])

    def Address(self, obj):
        return '<br/>'.join(c.account_address for c in account_contact.objects.filter(opportunity=obj.id).order_by('id')[:1])

我想问的是,在Django的管理后台,内联字段的标题显示的是函数的名字:联系、电话和地址。其实我想用自定义的文字来显示这些字段的标题,甚至想用中文来显示它们。我是不是漏掉了什么?

1 个回答

22

你需要在你的函数上定义一个叫做 short_description 的属性:https://docs.djangoproject.com/en/stable/ref/contrib/admin/actions/#writing-action-functions

举个例子:

Contact.short_description = 'foo'

撰写回答