I have a django
server that uses django-allauth
for handling authentication logic. I want to unregister
Sites framework from django-admin
, in other words, I do not want to see the Sites section inside django-admin:
Normally, I would do one of the two things:
django.contrib.sites
from INSTALLED_APPS
in settings.py
Sites
model from within one of my admin.py
files like so: admin.site.unregister(Sites)
I tried doing both options above. Here is the error when I unplug Sites app from INSTALLED_APPS
:
...
File "C:\path-to-my-project\venv\lib\site-packages\allauth\utils.py", line 13, in <module>
from django.contrib.sites.models import Site
File "C:\path-to-my-project\venv\lib\site-packages\django\contrib\sites\models.py", line 78, in <module>
class Site(models.Model):
File "C:\path-to-my-project\venv\lib\site-packages\django\db\models\base.py", line 113, in __new__
raise RuntimeError(
RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
django-allauth
is clearly using it in line 13.
Here is the error when I do admin.sites.unregister(Site)
:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Program Files\Python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\path-to-my-project\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\path-to-my-project\venv\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "C:\path-to-my-project\venv\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "C:\path-to-my-project\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "C:\path-to-my-project\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\path-to-my-project\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\path-to-my-project\venv\lib\site-packages\django\apps\registry.py", line 122, in populate
app_config.ready()
File "C:\path-to-my-project\venv\lib\site-packages\django\contrib\admin\apps.py", line 27, in ready
self.module.autodiscover()
File "C:\path-to-my-project\venv\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\path-to-my-project\venv\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Program Files\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\path-to-my-project\accounts\admin.py", line 94, in <module>
admin.site.unregister(Site)
File "C:\path-to-my-project\venv\lib\site-packages\django\contrib\admin\sites.py", line 153, in unregister
raise NotRegistered('The model %s is not registered' % model.__name__)
django.contrib.admin.sites.NotRegistered: The model Site is not registered
The issue is django-allauth
requires the Sites framework in order to work properly. Here is the official explanation:
settings.py (Important - Please note ‘django.contrib.sites’ is required as INSTALLED_APPS):
All I want is to hide the sites section from django-admin, as I have no use for it. I do not want to unplug or unregister, because I want django-allauth
's functionality intact.
How do I achieve this? What am I missing?
django-allauth
installedOne approach is to unregister the Site
admin on one of your AppConfig.ready()
functions like this:
from django.apps import AppConfig
class SomeAppConfig(AppConfig):
# ...
def ready(self):
from django.contrib import admin
from django.contrib.sites.models import Site
admin.site.unregister(Site)
The error happens because doing the unregister on one of your admin.py
will not guarantee that the register for the django.contrib.sites
has already finished. Doing this in a ready()
function will work because all apps are already registered along with their admins.