你能给一个 Django 应用程序一个详细的名称以供整个管理员使用吗? [英] Can you give a Django app a verbose name for use throughout the admin?

查看:27
本文介绍了你能给一个 Django 应用程序一个详细的名称以供整个管理员使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与您可以为出现在 Django 管理中的字段和模型提供详细名称一样,您可以为应用程序提供自定义名称吗?

In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?

推荐答案

Django 1.8+

根据 1.8 文档(和 当前文档),

新应用程序应避免default_app_config.相反,他们应该要求在 INSTALLED_APPS 中明确配置适当的 AppConfig 子类的虚线路径.

New applications should avoid default_app_config. Instead they should require the dotted path to the appropriate AppConfig subclass to be configured explicitly in INSTALLED_APPS.

示例:

INSTALLED_APPS = [
    # ...snip...
    'yourapp.apps.YourAppConfig',
]

然后更改您的 AppConfig,如下所示.

Then alter your AppConfig as listed below.

Django 1.7

正如 rhunwicks 对 OP 的评论所述,自 Django 1.7 起,现在可以开箱即用

As stated by rhunwicks' comment to OP, this is now possible out of the box since Django 1.7

摘自文档:>

# in yourapp/apps.py
from django.apps import AppConfig

class YourAppConfig(AppConfig):
    name = 'yourapp'
    verbose_name = 'Fancy Title'

然后将 default_app_config 变量设置为 YourAppConfig

then set the default_app_config variable to YourAppConfig

# in yourapp/__init__.py
default_app_config = 'yourapp.apps.YourAppConfig'

Django 1.7 之前

您可以通过在模型定义中定义 app_label 来为您的应用程序指定一个自定义名称.但是当 django 构建管理页面时,它会根据模型的 app_label 对模型进行哈希处理,因此如果您希望它们出现在一个应用程序中,则必须在应用程序的所有模型中定义此名称.

You can give your application a custom name by defining app_label in your model definition. But as django builds the admin page it will hash models by their app_label, so if you want them to appear in one application, you have to define this name in all models of your application.

class MyModel(models.Model):
        pass
    class Meta:
        app_label = 'My APP name'

这篇关于你能给一个 Django 应用程序一个详细的名称以供整个管理员使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆