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?.
admin.py is executed whenever your django loads URLconf from urls.py, the autodiscover() will search for all the apps in INSTALLED_APPS one by one and executes the code in that file.
The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.
Django provides a default admin interface which can be used to perform create, read, update and delete operations on the model directly. It reads set of data that explain and gives information about data from the model, to provide an instant interface where the user can adjust contents of the application .
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.
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With