如何在 Django Admin 中翻译模型标签?

发布于 2024-08-25 15:57:28 字数 105 浏览 7 评论 0原文

我可以翻译 Django Admin 除了模型标签之外的内容,因为我不知道如何在 Django Admin 中翻译模型标签。

那么,如何在 Django Admin 中翻译模型标签?

I could translate Django Admin except a model label because I don't know how to translate a model label in Django Admin.

So, how can I translate a model label in Django Admin?

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

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

发布评论

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

评论(4

┈┾☆殇 2024-09-01 15:57:28

Look at the Meta options verbose_name and verbose_name_plural, both of which are translatable.

擦肩而过的背影 2024-09-01 15:57:28

您应该在所有模型的元数据中使用 ugettext_lazy util

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Book(models.Model):
    ...

    class Meta:
        verbose_name = _("My Book")
        verbose_name_plural = _("My Books")

You should use the ugettext_lazy util in the Meta of all your models

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Book(models.Model):
    ...

    class Meta:
        verbose_name = _("My Book")
        verbose_name_plural = _("My Books")
宛菡 2024-09-01 15:57:28

您应该使用 gettext_lazy() 和将其设置为 verbose_nameverbose_name_plural 在 Django Admin 中翻译模型标签,如图所示以下。 *您可以查看我的回答详细解释如何在Django中翻译:

# "models.py"

from django.db import models
from django.utils.translation import gettext_lazy as _

class Person(models.Model):
    ...

    class Meta:
        verbose_name = _("person") # Here
        verbose_name_plural = _("persons") # Here

You should use gettext_lazy() and set it to verbose_name and verbose_name_plural to translate a model label in Django Admin as shown below. *You can see my answer explaining how to translate in Django in detail:

# "models.py"

from django.db import models
from django.utils.translation import gettext_lazy as _

class Person(models.Model):
    ...

    class Meta:
        verbose_name = _("person") # Here
        verbose_name_plural = _("persons") # Here
少跟Wǒ拽 2024-09-01 15:57:28

您也可以覆盖 admin.ModelAdmin 仅在管理中自定义模型名称,而不更改模型元

def __init__(self, model: type, admin_site: AdminSite) -> None:
        super().__init__(model, admin_site)
        self.opts.verbose_name = 'your custom model name'
        self.opts.verbose_name_plural = 'your custom title plural'

also you can override admin.ModelAdmin to customize model name in admin only, without changing Model meta

def __init__(self, model: type, admin_site: AdminSite) -> None:
        super().__init__(model, admin_site)
        self.opts.verbose_name = 'your custom model name'
        self.opts.verbose_name_plural = 'your custom title plural'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文