Django:如何在使用 django-allauth 时从 Django Admin 隐藏站点框架?

Django: How to hide Sites framework from Django Admin while using django-allauth?

背景

我有一个 django 服务器,它使用 django-allauth 来处理身份验证逻辑。我想 unregister 来自 django-admin 的站点框架,换句话说,我不想看到 django-admin 中的站点部分:

我试过的

通常,我会做以下两件事之一:

  1. settings.py
  2. 中的 INSTALLED_APPS 拔下 django.contrib.sites
  3. 从我的一个 admin.py 文件中注销 Sites 模型,如下所示:admin.site.unregister(Sites)

问题

我尝试了以上两个选项。这是我从 INSTALLED_APPS 拔下 Google 协作平台应用程序时出现的错误:

...
  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 显然在第 13 行中使用了它。

这是我执行 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

问题是 django-allauth 需要站点框架才能正常工作。这里是 the official explanation:

settings.py (Important - Please note ‘django.contrib.sites’ is required as INSTALLED_APPS):

我想要的

我只想对 django-admin 隐藏站点部分,因为我用不着它。我不想拔出或注销,因为我希望 django-allauth 的功能完好无损。 我如何实现这一目标?我错过了什么?

重现错误的步骤

  1. 创建一个安装了 django-allauth 的 Django 项目
  2. 创建超级用户并登录到 Django admin
  3. 尝试对 Django 管理员隐藏站点框架

一种方法是在您的 AppConfig.ready() 功能之一上注销 Site 管理员,如下所示:

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)

发生错误是因为在您的 admin.py 之一上取消注册并不能保证 django.contrib.sites 的注册已经完成。在 ready() 函数中执行此操作将有效,因为所有应用程序都已与其管理员一起注册。