By default, the Django admin dashboard looks like this for me:
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 beenregistered 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_listmysite = MyAdminSite()
admin.site = mysite
sites.site = mysite
New look and feel:
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?
With the code below in settings.py
, you can reorder and hide the apps including AUTHENTICATION AND AUTHORIZATION(auth)
and models in admin pages. *I use Django 4.1.7 and you can see the original get_app_list() in GitHub and you can see my answer demonstrating how the code below works:
# "settings.py"ADMIN_ORDERING = (('app2', ('Model3', 'Model1', 'Model2')),('auth', ('User', 'Group')),('app1', ('Model2', 'Model3', 'Model1'))
)from django.contrib import admindef get_app_list(self, request, app_label=None):app_dict = self._build_app_dict(request, app_label)if not app_dict:returnNEW_ADMIN_ORDERING = []if app_label:for ao in ADMIN_ORDERING:if ao[0] == app_label:NEW_ADMIN_ORDERING.append(ao)breakif 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_listadmin.AdminSite.get_app_list = get_app_list