:
मुझे लगता है कि आप बेहतर नियंत्रण देने के बारे में है (संपादन Django 1.7+ के बाद पुराना हो, जरूरी नहीं अधिक, Alasdair का जवाब देखें)। 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 मॉड्यूल लोड और चुपचाप असफल जब नहीं मिला होगा।
# 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()
कॉल करने के लिए की जरूरत नहीं है: अब, ऐसे मामले हैं जब आप वास्तव में है कि इस तरह जब अपने खुद के AdminSite
का उपयोग कर के रूप में नहीं करना चाहते हैं।
अन्य बार भी जब आप केवल व्यवस्थापक के माध्यम से अपनी परियोजनाओं के ऐप्स का सबसेट देखना या संपादित करना चाहते हैं, और autodiscovery()
पर कॉल करने से आपको ऐसा करने में सक्षम नहीं होगा।
स्रोत
2012-10-09 21:09:15
मुझे समझ में नहीं आता कि इसे ऐप्स लोड करने की आवश्यकता क्यों है। क्या 'INSTALLED_APPS' में ऐप्स प्रत्येक बार' सेटिंग्स.py' निष्पादित नहीं होते हैं (उदा। सर्वर लॉन्च करते समय)? – Bentley4
'autodiscover' ऐप्स लोड नहीं कर रहा है, यह प्रत्येक ऐप के लिए 'admin.py' आयात कर रहा है। – Alasdair
@ बेंटले 4 हां जैसे अलास्डेयर ने कहा, यह केवल प्रत्येक ऐप के 'admin.py' को आयात कर रहा है। –