简体   繁体   English

覆盖Django admin中任何外部软件包的默认文本标签

[英]Override default text label for any external packages in Django admin

It's the classic way to describe Modules in Django Admin, and it works great! 这是在Django Admin中描述模块的经典方法,而且效果很好!

at bookshelf/apps.py 在bookshelf / apps.py

from django.apps import AppConfig
class BOOKConfig(AppConfig):
    name = 'bookshelf'
    verbose_name = "Your Book"

at bookshelf/__init__.py : bookshelf/__init__.py

default_app_config = 'bookshelf.apps.BOOKConfig'

BUT when you want to override titles of external modules (eg like packages from https://djangopackages.org/ ), what's the right way to override the default name at this sections and the items inside that? 但是,当您想覆盖外部模块的标题(例如,来自https://djangopackages.org/的软件包)时,在此部分以及其中的项目覆盖默认名称的正确方法是什么?

As the docs say , new applications should avoid default_app_config. 文档所述 ,新应用程序应避免使用default_app_config。

Instead of adding default_app_config to the app's __init__.py , just use the dotted path to the app config in INSTALLED_APPS . 无需在应用程序的__init__.py中添加default_app_config ,而只需在INSTALLED_APPS使用虚线即可指向应用程序配置。

INSTALLED_APPS = [
    ...
    'bookshelf.apps.BOOKConfig'
    ...
]

For a third-party app you can do the same thing. 对于第三方应用程序,您可以执行相同的操作。 Create an apps.py somewhere in your project (eg alongside myproject/settings.py ), and create an app config. 在您的项目中的某个位置(例如,与myproject/settings.py一起)创建一个apps.py ,并创建一个应用程序配置。

from third_party_app..apps import ThirdPartyConfig

class MyThirdPartyConfig(ThirdPartyConfig):
    verbose_name = "Customized app name"

If the app doesn't have an App Config class, then subclass AppConfig and make sure you set name . 如果应用程序没有App Config类,则子类AppConfig并确保设置了name

from django.apps import AppConfig
class MyThirdPartyConfig(AppConfig):
    name = 'third_party_app'
    verbose_name = "Customized app name"

Then use the path to your app config class in INSTALLED_APPS instead of the app name/default app config. 然后,使用INSTALLED_APPS应用程序配置类的路径,而不是应用程序名称/默认应用程序配置。

INSTALLED_APPS = [
    ...
    'myproject.apps.MyThirdPartyConfig,
    ...
]

See the for application users section of the docs for another example. 有关其他示例,请参见文档的“ 面向应用程序用户”部分。

Suppose you have a model like this: 假设您有一个这样的模型:

class Stuff(models.Model):
    class Meta:
        verbose_name = u'The stuff'
        verbose_name_plural = u'The bunch of stuff'

You have verbose_name, however you want to customise app_label too for different display in admin. 您有verbose_name,但是您也想自定义app_label以便在admin中进行不同的显示。 Unfortunatelly having some arbitrary string (with spaces) doesn't work and it's not for display anyway. 不幸的是,使用任意字符串(带空格)是行不通的,而且无论如何也不能显示。

Turns out that the admin uses app_label. 原来,管理员使用app_label。 title () for display so we can make a little hack: str subclass with overriden title method: title()进行显示,因此我们可以做些改动:str子类,带有重写的title方法:

class string_with_title(str):
    def __new__(cls, value, title):
        instance = str.__new__(cls, value)
        instance._title = title
        return instance

    def title(self):
        return self._title

    __copy__ = lambda self: self
    __deepcopy__ = lambda self, memodict: self

Now we can have the model like this: 现在我们可以有这样的模型:

class Stuff(models.Model):
    class Meta:
        app_label = string_with_title("stuffapp", "The stuff box")
        # 'stuffapp' is the name of the django app
        verbose_name = 'The stuff'
        verbose_name_plural = 'The bunch of stuff'

Original Ionel's post https://blog.ionelmc.ro/2011/06/24/custom-app-names-in-the-django-admin/ 原始的Ionel帖子https://blog.ionelmc.ro/2011/06/24/custom-app-names-in-the-django-admin/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM