Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin unregister Sites

I've been trying to unregister the admin for sites in django by doing the following:

from django.contrib.sites.models import Site

admin.site.unregister(Site)

However this gives me an error stating that "Site" is not registered (even though it showed up in admin before).

If I try doing the following I get no errors but "Site" stays in the admin:

from django.contrib.sites.models import Site

admin.site.register(Site)
admin.site.unregister(Site)

I need the sites app and cannot take it out of INSTALLED_APPS in settings. However the admin for it is utterly useless to me. Any ideas on what I am doing wrong here?

Thanks!

like image 450
Max Smith Avatar asked May 25 '17 22:05

Max Smith


People also ask

How do I unregister a Django model?

You can unregister default models in admin.py of your app by using unregister .

How do I restrict access to parts of Django admin?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

What is admin site register in Django?

The Django admin is an automatically-generated user interface for Django models. The register function is used to add models to the Django admin so that data for those models can be created, deleted, updated and queried through the user interface.

How can I remove extra's from Django admin panel?

You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Literally saved my life!


1 Answers

The order of your INSTALLED_APPS setting is important.

When Django starts it will import the applications in INSTALLED_APPS in the order they are defined (https://docs.djangoproject.com/en/1.11/ref/applications/#how-applications-are-loaded). In your example above you were unregistering Site before Django had the chance to register is it.

There isn't much you can do in terms of troubleshooting except for reading the logs very carefully. After staring at them for a while you either become one with Django and understand it all, or you come here like the rest of us ;-)

My INSTALLED_APPS usually start with the django.contrib apps, then any 3rd-party applications, then my apps at the bottom. I only change this if I have a very good reason to.

like image 150
Geotob Avatar answered Nov 15 '22 11:11

Geotob