68

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).

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Anton Koval'
  • 4,863
  • 5
  • 31
  • 44

6 Answers6

118

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

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.unregister(Site)
Steve Jalim
  • 11,989
  • 1
  • 37
  • 54
  • 44
    You will need to import them (or I did): from django.contrib.sites.models import Site from django.contrib.auth.models import * – Vernon Aug 16 '10 at 11:47
  • This method that will not work if you load your apps before the django apps. I have to do this to ensure my apps' custom registration templates overwrite the default django registration templates in 3.2. This will result in `NotRegistered('The model %s is not registered' % model.__name__)` errors if the app with the admin.py is loaded before the django apps. @user2745509's solution below works regardless of INSTALLED_APPS order, but is much less clean. – Jon Nov 09 '21 at 20:57
10

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: Issue with Django admin registering an inline user profile admin

Schmalitz
  • 420
  • 1
  • 8
  • 16
  • Yes, this helps. Your app must be below other ones. Otherwise, the user model is still from the original app(contrib.auth), not yours. – Dat TT Apr 19 '20 at 02:51
8

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)
user2745509
  • 411
  • 4
  • 5
  • 1
    This is the only method that will work if you, like me, load your local apps before your django apps. I have to do this to ensure my local app's custom registration templates overwrite the default django registration templates in 3.2. The accepted answer will result in `NotRegistered('The model %s is not registered' % model.__name__)` errors if your app is loaded before the django apps. – Jon Nov 09 '21 at 20:53
2

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
...
cdosborn
  • 3,111
  • 29
  • 30
  • This is a best answer. Not sure where to add it, because in AppConfig.ready it is to late. I have added this on the top of the admin.py in my 1st app registered in INSTALLED_APPS. Additionaly I have added condition to let Users untouched, something like: `if not app_config.name == 'django.contrib.auth' # before the model loop` – mirek Mar 12 '21 at 12:12
1

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',
)
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
0
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.

onepoordeveloper
  • 199
  • 1
  • 5
  • 15