为什么在使用admin时Django中没有自动调用admin.autodiscover()?为什么要显式调用它? [英] Why isn't admin.autodiscover() called automatically in Django when using the admin, why was it designed to be called explicitly?

查看:74
本文介绍了为什么在使用admin时Django中没有自动调用admin.autodiscover()?为什么要显式调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有将 admin.autodiscover()放在urls.py中的情况下,管理页面会显示您无权编辑任何内容(

Without putting admin.autodiscover() in urls.py the admin page shows You don't have permission to edit anything (See SO thread).

为什么会这样?如果您始终需要添加 admin.autodiscover()来使用admin编辑信息,即使您具有安全性的超级用户名和密码,为什么Django开发人员也不会触发 admin.autodiscover()自动吗?.

Why is this so? If you always need to add admin.autodiscover() to edit information using the admin even though you have a superuser name and password for security why didn't the Django developers trigger admin.autodiscover() automatically?.

推荐答案

(在Django 1.7+之后过时,不再需要,请参见Alasdair的回答)

(edit: Obsoleted after Django 1.7+, not necessary more, see Alasdair's answer)

我认为这是为您提供更好的控制.考虑 contrib.admin.autodiscover :

I think it's about giving you finer control. Consider the code of contrib.admin.autodiscover:

def autodiscover():
    """
    Auto-discover INSTALLED_APPS admin.py modules and fail silently when
    not present. This forces an import on them to register any admin bits they
    may want.
    """

    import copy
    from django.conf import settings
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's admin module.
        try:
            before_import_registry = copy.copy(site._registry)
            import_module('%s.admin' % app)
        except:
            # Reset the model registry to the state before the last import as
            # this import will have to reoccur on the next request and this
            # could raise NotRegistered and AlreadyRegistered exceptions
            # (see #8245).
            site._registry = before_import_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have an admin module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'admin'):
                raise

因此它将自动加载INSTALLED_APPS admin.py模块,并且在找不到时会静默失败.现在,有些情况下您实际上并不希望这样做,例如使用自己的 AdminSite :

So it will automatically load the INSTALLED_APPS admin.py modules and fail silently when not found. Now, there are cases when you actually don't want that such as when using your own AdminSite:

# urls.py
from django.conf.urls import patterns, url, include
from myproject.admin import admin_site

urlpatterns = patterns('',
    (r'^myadmin/', include(admin_site.urls)),
)

在这种情况下,您不需要调用 autodiscovery().

in this case, you don't need to call autodiscovery().

还有其他时候,您只想通过admin查看或编辑项目的部分应用程序,而调用 autodiscovery()并不能使您做到这一点.

There are also other times when you only want to see or edit a subset of apps of your projects through admin, and calling autodiscovery() would not enable you to do that.

这篇关于为什么在使用admin时Django中没有自动调用admin.autodiscover()?为什么要显式调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆