简体   繁体   English

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

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

Background背景

I have a django server that uses django-allauth for handling authentication logic.我有一个使用django-allauth处理身份验证逻辑的django服务器。 I want to unregister Sites framework from django-admin , in other words, I do not want to see the Sites section inside django-admin:我想从django-admin unregister站点框架,换句话说,我不想看到 django-admin 中的站点部分:

Django-admin 当前状态

What I tried我试过的

Normally, I would do one of the two things:通常,我会做以下两件事之一:

  1. Unplug django.contrib.sites from INSTALLED_APPS in settings.pysettings.py中的INSTALLED_APPS中拔下django.contrib.sites
  2. Unregister Sites model from within one of my admin.py files like so: admin.site.unregister(Sites)从我的admin.py文件之一中取消注册Sites model,如下所示: admin.site.unregister(Sites)

The problem问题

I tried doing both options above.我尝试执行上述两个选项。 Here is the error when I unplug Sites app from INSTALLED_APPS :这是我从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. django-allauth显然在第 13 行使用了它。

Here is the error when I do admin.sites.unregister(Site) :这是我执行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.问题是django-allauth需要站点框架才能正常工作。 Here is the official explanation :以下是官方解释

settings.py (Important - Please note 'django.contrib.sites' is required as INSTALLED_APPS): settings.py(重要 - 请注意 'django.contrib.sites' 是 INSTALLED_APPS 所必需的):

What I want我想要的是

All I want is to hide the sites section from django-admin, as I have no use for it.我想要的只是从 django-admin 隐藏站点部分,因为我没有用它。 I do not want to unplug or unregister, because I want django-allauth 's functionality intact.我不想拔掉或取消注册,因为我希望django-allauth的功能完好无损。 How do I achieve this?我如何实现这一目标? What am I missing?我错过了什么?

Steps to reproduce the error重现错误的步骤

  1. Create a Django project with django-allauth installed创建一个安装了django-allauth的 Django 项目
  2. Create a superuser and log into Django admin创建超级用户并登录 Django admin
  3. Try to hide the sites framework from Django admin尝试对 Django 管理员隐藏站点框架

One approach is to unregister the Site admin on one of your AppConfig.ready() functions like this:一种方法是在您的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)

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.发生错误是因为在您的 admin.py 之一上进行注销将不能保django.contrib.sites admin.py的注册已经完成。 Doing this in a ready() function will work because all apps are already registered along with their admins.ready() function 中执行此操作将起作用,因为所有应用程序都已与其管理员一起注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM