手动自定义排列 Django Admin 主页 APP 列表顺序

栏目: Python · 发布时间: 5年前

内容简介:Django Admin 主页 APP 列表默认是按照By default, it displays all the apps in

WHY

Django Admin 主页 APP 列表默认是按照 字母顺序 进行排列的,想根据 APP 使用来排序。

https://docs.djangoproject.com/en/2.1/intro/tutorial07/

By default, it displays all the apps in INSTALLED_APPS that have been registered with the admin application, in alphabetical order .

https://docs.djangoproject.com/en/2.1/_modules/django/contrib/admin/sites/

def get_app_list(self, request):
    """
    Return a sorted list of all the installed apps that have been
    registered in this site.
    """
    app_dict = self._build_app_dict(request)

    # Sort the apps alphabetically.
    app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())

    # Sort the models alphabetically within each app.
    for app in app_list:
        app['models'].sort(key=lambda x: x['name'])

    return app_list

网上有多种实现方案:

  • 每个 APP model 里为 Meta 类下的 verbose_name_plural 字段添加 数字前缀 1
  • 重写 admin/base.html 模板页面 2
  • 重写 AdminSite 类的 index() 方法 3

本文使用第三种:重写 AdminSite 类的 index() 方法

HOW

Django 源码 Admin 主页渲染的 app_list 列表是由 get_app_list() 方法按 字母排序 后返回的结果

所以只需要对 app_list 进行 排序 即可

https://docs.djangoproject.com/en/2.1/_modules/django/contrib/admin/sites/

def index(self, request, extra_context=None):
    """
    Display the main admin index page, which lists all of the installed
    apps that have been registered in this site.
    """
    app_list = self.get_app_list(request)

    context = {
        **self.each_context(request),
        'title': self.index_title,
        'app_list': app_list,
        **(extra_context or {}),
    }

    request.current_app = self.name

    return TemplateResponse(request, self.index_template or 'admin/index.html', context)

首先参考官方文档覆盖 Django Admin 的 AdminSite 类:

https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#overriding-the-default-admin-site

项目名称 zerone

zerone/ 子目录下创建 zerone/admin.py 文件,重写 AdminSiteindex() 方法:

from django.contrib import admin

class MyAdminSite(admin.AdminSite):

    def index(self, request, extra_context=None):
        if extra_context is None:
            extra_context = {}

        app_list = self.get_app_list(request)
        app_dict = self._build_app_dict(request)

        # dict_keys(['equipment', 'liveos', 'clone', 'oauth2_provider', 'auth'])
        print(app_list, app_dict.keys())

        extra_context['app_list'] = app_list
        return super(MyAdminSite, self).index(request, extra_context)

创建 zerone/apps.py 文件,自定义 AdminConfig 配置名称:

from django.contrib.admin.apps import AdminConfig

class MyAdminConfig(AdminConfig):
    default_site = 'zerone.admin.MyAdminSite'

使用刚在 zerone/apps.py 自定义的 APP 配置名称,替换默认的 django.contrib.admin

修改 zerone/settings.py

INSTALLED_APPS = [
    ...
    'zerone.apps.MyAdminConfig',  # replaces 'django.contrib.admin'
    ...
]

重写 AdminSiteindex() 方法之前,先查看一下默认 app_dict 包含的 所有 APP 名称:

app_list = self.get_app_list(request)
app_dict = self._build_app_dict(request)
print(app_list, app_dict.keys())

# dict_keys(['equipment', 'liveos', 'clone', 'oauth2_provider', 'auth'])

默认 get_app_list() 按照字母排序的 APP 顺序是:

'auth',
'oauth2_provider',
'clone',
'equipment',
'liveos'

然后根据上面显示的 所有 APP 名称,自定义 APP 的排序列表:

apps_order_list = ['equipment', 'liveos', 'clone', 'oauth2_provider', 'auth']

然后就是根据自定义的 apps_order_list 对默认的 app_list 进行排序:

from django.contrib import admin

apps_order_list = ['equipment', 'liveos', 'clone', 'oauth2_provider', 'auth']
apps_order_dict = { app: index for index, app in enumerate(apps_order_list) }

class MyAdminSite(admin.AdminSite):

    def index(self, request, extra_context=None):
        if extra_context is None:
            extra_context = {}

        app_list = self.get_app_list(request)
        app_dict = self._build_app_dict(request)

        # dict_keys(['equipment', 'liveos', 'clone', 'oauth2_provider', 'auth'])
        print(app_dict.keys())

        # https://stackoverflow.com/questions/15650348/sorting-a-list-of-dictionaries-based-on-the-order-of-values-of-another-list
        # https://docs.python.org/3/howto/sorting.html
        app_list.sort(key=lambda element_dict: apps_order_dict[element_dict["app_label"]])

        # print(app_list)

        extra_context['app_list'] = app_list
        return super(MyAdminSite, self).index(request, extra_context)

DONE

reference

https://docs.python.org/3/howto/sorting.html

You can also use the list.sort() method. It modifies the list in-place ( and returns None to avoid confusion ). Usually it’s less convenient than sorted() - but if you don’t need the original list, it’s slightly more efficient .

Sorting a list of dictionaries based on the order of values of another list

Defining a custom app_list in django admin index page

Ordering admin.ModelAdmin objects in Django Admin


以上所述就是小编给大家介绍的《手动自定义排列 Django Admin 主页 APP 列表顺序》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Haskell Programming from first principles

Haskell Programming from first principles

Christopher Allen、Julie Moronuki / 2015 / USD 59.00

I am writing this book because I had a hard time learning Haskell. It doesn't have to be that way. I've spent the last couple years actively teaching Haskell online and in person. Along the way, I ......一起来看看 《Haskell Programming from first principles》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试