Why isn't admin.autodiscover() called automatically in Django when using the admin, why was it designed to be called explicitly?

狂风中的少年 提交于 2021-02-17 19:13:16

问题


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

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?.


回答1:


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

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

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)),
)

in this case, you don't need to call 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.




回答2:


Before Django 1.7, the recommendation was to put the admin.autodiscover() call in urls.py. That allowed it to be disabled if necessary. Requiring admin.autodiscover() instead of calling it automatically was an example of the Python philosophy 'Explicit is better than implicit' in action. Remember, the django.contrib.admin app is optional, it is not installed on every site, so it wouldn't make sense to always run autodiscover.

Most of the time autodiscover works well enough. However if you require more control, you can manually import specific apps' admin files instead. For example, you might want to register multiple admin sites with different apps in each.

App loading was refactored in Django 1.7. The autodiscover() was moved to the admin app's default app config. That means that autodiscover now runs when the admin app is loaded, and there's no need to add admin.autodiscover() to your urls.py. If you do not want autodiscovery, you can now disable it by using the SimpleAdminConfig instead.




回答3:


Django doesn't require you to use django.contrib.admin on every site - it's not a core module.



来源:https://stackoverflow.com/questions/12807220/why-isnt-admin-autodiscover-called-automatically-in-django-when-using-the-adm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!