How can reorder and hide the apps and models in Django admin
You can reorder and hide the apps and models in admin pages.
For example, there are the same 3 models in cms/models.py
as shown below:
# "cms/models.py
from django.db import models
class Page(models.Model):
pass
class PageSection(models.Model):
pass
class Seo(models.Model):
pass
Then, there are the same 3 admins in app1/admin.py
and app2/admin.py
as shown below:
# "cms/admin.py
from django.contrib import admin
from .models import Page, PageSection, Seo
class PageAdmin(admin.ModelAdmin):
pass
class PageSectionAdmin(admin.ModelAdmin):
pass
class SeoAdmin(admin.ModelAdmin):
pass
admin.site.register(Page,PageAdmin)
admin.site.register(PageSection,PageSectionAdmin)
admin.site.register(Seo,SeoAdmin)
Then, apps including AUTHENTICATION AND AUTHORIZATION(auth)
are displayed in Home
admin page
Now, set ADMIN_ORDERING
and treat it in overridden get_app_list()
in settings.py
as shown below. *You can see the original get_app_list() in GitHub:
# "core/settings.py"
from django.contrib import admin
ADMIN_ORDERING = (
('cms', (
'Page',
'PageSection',
'Seo'
)),
('auth', (
'User','Group'
))
)
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
Then, the order of apps and the models is changed in Home
admin page. The order of them is also changed in other admin pages not just in Home
admin page: