Set ordering of Apps and models in Django admin dashboard

Viewed 3140

By default, the Django admin dashboard looks like this for me:

enter image description here

I want to change the ordering of models in Profile section, so by using codes from here and here I was able to change the ordering of model names in Django admin dashboard:

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

new look and feel:

enter image description here

But as you see, I have lost the AUTHENTICATION AND AUTHORIZATION section; What should I do to have all the sections and at the same time have my own custom ordering for Profile section?

2 Answers

Call get_app_list() with super first, like that:

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

What u can do is just override the get_app_list method.

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
Related