python 在Django管理面板中设置应用和模型的顺序

x0fgdtte  于 2023-05-16  发布在  Python
关注(0)|答案(3)|浏览(216)

默认情况下,Django管理 Jmeter 板看起来像这样:

我想在Profile部分更改模型的顺序,所以通过使用herehere中的代码,我可以在Django管理面板中更改模型名称的顺序:

class MyAdminSite(admin.AdminSite):
    def get_app_list(self, request):
        """
        Return a sorted list of all the installed apps that have been
        registered in this site.
        """
        ordering = {
            "Users": 1,
            "Permissions": 2,
            "Activities": 3,
        }
        app_dict = self._build_app_dict(request)
        # a.sort(key=lambda x: b.index(x[0]))
        # 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: ordering[x['name']])

        return app_list

mysite = MyAdminSite()
admin.site = mysite
sites.site = mysite

新的外观和感觉:

但是正如你所看到的,我已经丢失了AUTHENTICATION AND AUTHORIZATION部分;我应该做些什么才能拥有所有的部分,同时拥有自己的Profile部分的自定义订购?

ryevplcw

ryevplcw1#

首先使用super调用get_app_list(),如下所示:

class MyAdminSite(admin.AdminSite):
def get_app_list(self, request):
    all_list = super().get_app_list(request)
    # reorder the app list as you like
    return app_list

mysite = MyAdminSite()
admin.site = mysite
sites.site = mysite
csbfibhn

csbfibhn2#

你能做的就是覆盖get_app_list方法。

def get_app_list(self, request):
    """
    Return a sorted list of all the installed apps that have been
    registered in this site.
    """
    # Retrieve the original list
    app_dict = self._build_app_dict(request)
    app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())

    # Sort the models customably within each app.
    for app in app_list:
        if app['app_label'] == 'auth':
            ordering = {
                'Users': 1,
                'Groups': 2
            }
            app['models'].sort(key=lambda x: ordering[x['name']])

    return app_list

admin.AdminSite.get_app_list = get_app_list
46qrfjad

46qrfjad3#

使用下面的代码在settings.py中,您可以重新排序和隐藏应用程序,包括管理页面中的AUTHENTICATION AND AUTHORIZATION(auth)和模型。* 我使用Django 4.1.7,你可以在GitHub中看到原始的get_app_list(),你可以看到我的答案,演示下面的代码是如何工作的:

# "settings.py"

ADMIN_ORDERING = (
    ('app2', ('Model3', 'Model1', 'Model2')),
    ('auth', ('User', 'Group')),
    ('app1', ('Model2', 'Model3', 'Model1'))
)

from django.contrib import admin

def get_app_list(self, request, app_label=None):
    app_dict = self._build_app_dict(request, app_label)
    
    if not app_dict:
        return
        
    NEW_ADMIN_ORDERING = []
    if app_label:
        for ao in ADMIN_ORDERING:
            if ao[0] == app_label:
                NEW_ADMIN_ORDERING.append(ao)
                break
    
    if not app_label:
        for app_key in list(app_dict.keys()):
            if not any(app_key in ao_app for ao_app in ADMIN_ORDERING):
                app_dict.pop(app_key)
    
    app_list = sorted(
        app_dict.values(), 
        key=lambda x: [ao[0] for ao in ADMIN_ORDERING].index(x['app_label'])
    )
     
    for app, ao in zip(app_list, NEW_ADMIN_ORDERING or ADMIN_ORDERING):
        if app['app_label'] == ao[0]:
            for model in list(app['models']):
                if not model['object_name'] in ao[1]:
                    app['models'].remove(model)
        app['models'].sort(key=lambda x: ao[1].index(x['object_name']))
    return app_list

admin.AdminSite.get_app_list = get_app_list

相关问题