Remove default apps from Django-admin

PythonDjangoDjango Admin

Python Problem Overview


By default, in Django-admin there is Users, Groups, and Sites apps. How can I remove Groups and Sites?

I tried to remove admin.autodiscover() from root urls. Then, when I added something like admin.site.register(User, UserAdmin) somewhere in my app models I got an AlreadyRegistered exception (this is fairly right - models users already registered in django.contrib.auth).

Python Solutions


Solution 1 - Python

In an admin.py you know will definitely be loaded, try:

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.unregister(Site)

Solution 2 - Python

In addition to the above double check your ordering of "INSTALLED_APPS" in "settings.py"

INSTALLED_APPS = [
    # django apps first
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # custom apps below
    'my_app'
]

Otherwise it will cause an error. See here: https://stackoverflow.com/questions/3729866/issue-with-django-admin-registering-an-inline-user-profile-admin

Solution 3 - Python

To get rid of Users and Groups I had to do in admin.py:

from django.contrib import admin
# Need to import this since auth models get registered on import.
import django.contrib.auth.admin
import django.contrib.auth.models
from django.contrib import auth

admin.site.unregister(auth.models.User)
admin.site.unregister(auth.models.Group)

Solution 4 - Python

Loop through all apps, and unregister any models they have registered.

from django.apps import apps


# De-register all models from other apps
for app_config in apps.get_app_configs():
    for model in app_config.get_models():
        if admin.site.is_registered(model):
            admin.site.unregister(model)


# Register only those models you want
...

Solution 5 - Python

If you got:

> django.contrib.admin.sites.NotRegistered: The model Group is not > registered

Then make sure that your INSTALLED_APPS in proper order like this:

enter code hereINSTALLED_APPS = (
# [1] Django apps
'django.contrib.auth',
...

# [2] your custom apps
'anyproject.anytuff',
)

Solution 6 - Python

from django.apps import apps

for model in apps.get_models():
    if model.__name__ and admin.site.is_registered(model):
        admin.site.unregister(model)

this will unregister all models depending upon the position of app where this code is placed and it's order inside INSTALLED_APPS so make sure the apps you want in your admin are placed after the app in which this code resides.

For Example: if this code is placed inside users app, it will unregister all models before users and all models after users can be registered.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnton Koval'View Question on Stackoverflow
Solution 1 - PythonSteve JalimView Answer on Stackoverflow
Solution 2 - PythonSchmalitzView Answer on Stackoverflow
Solution 3 - Pythonuser2745509View Answer on Stackoverflow
Solution 4 - PythoncdosbornView Answer on Stackoverflow
Solution 5 - PythonYerkebulan DuisebayView Answer on Stackoverflow
Solution 6 - PythononepoordeveloperView Answer on Stackoverflow