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

后端 未结 3 459
旧时难觅i
旧时难觅i 2021-02-05 17:14

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

3条回答
  •  不思量自难忘°
    2021-02-05 17:51

    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.

提交回复
热议问题