Django管理界面:如何覆盖模型的verbose_name?

9

我有两个模型(Country和State),并且我只为Country创建了一个ModelAdmin,并使用TabularInline为State提供支持。

class StateInline(admin.TabularInline):
    model = State

class CountryAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['name']}),
    ]
    inlines = [StateInline]

admin.site.register(Country, CountryAdmin)

我该如何覆盖Country模型的verbose_name Meta属性?我想将菜单中的表单名称改为“Countries / States”,而不仅仅是“Countries”?
1个回答

14

在你的模型中使用元选项(Meta)

from django.utils.tranlation import gettext_lazy as _

class Country(models.Model):
    # your fields
    class Meta:
        verbose_name = _("Country / State")
        verbose_name_plural = _("Countries / States")

3
我避免使用这种方法,猜测模型的verbose_name在管理菜单以外的地方被使用。但是在没有更好的解决方案之前,我会使用它。理想的解决方案是ModelAdmin具有verbose_name覆盖,就像TabularInline一样。谢谢你的回答... - jrvidotti
我认为这种解决方案不存在,但你可以在Django网站上报告一个功能 :) - Leandro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接