Django: Remove default entries from admin menu

Posted by ads' corner on Sunday, 2020-11-29
Posted in [Django][Python]

The Django administration site comes with a couple of default entries, depending on which apps and middleware is installed.

Usually there is at least Users and Groups, and if the quite common “allauth” is installed, then there is also Site, SocialApp, SocialAccount, and SocialToken. They are not always necessary, especially when no Social Login is used. Or why have the “Site” administration when only Site=1 is used?

With a few tricks these can be removed from the admin menu.

For doing that, settings.py is not the right place - when this file is loaded the app is not yet entirely activated. I picked urls.py in my application directory.

To deactivate the admin pages it needs two things: import the module, and unregister the page:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# unregister certain apps from the admin menu
from django.contrib import admin
from django.contrib.auth.models import User, Group
from django.contrib.sites.models import Site
from allauth.socialaccount.models import SocialApp, SocialAccount, SocialToken

admin.site.unregister(Group)
admin.site.unregister(Site)
admin.site.unregister(SocialApp)
admin.site.unregister(SocialAccount)
admin.site.unregister(SocialToken)

That’s it. The 5 pages are no longer shown in the admin menu.


Categories: [Django] [Python]