2023年11月29日发(作者:)
django之setup()
#django包的__init__.py包含setup函数
def setup():
"""
Configure the settings (this happens as a side effect of accessing the
first setting), configure logging and populate the app registry.
"""
from import apps
from import settings
from import configure_logging
configure_logging(G_CONFIG, G)
te(LED_APPS)#apps对象安装app
def populate(self, installed_apps=None):
"""
Loads application configurations and models.
This method imports each application module and then each model module.
It is thread safe and idempotent, but not reentrant.
"""
if :
return
# populate() might be called by two threads in parallel on servers
# that create threads before initializing the WSGI callable.
with self._lock:
if :
return
# app_config should be pristine, otherwise the code below won't
# guarantee that the order matches the order in INSTALLED_APPS.
if _configs:
raise RuntimeError("populate() isn't reentrant")
# Load app configs and app modules.
for entry in installed_apps:
if isinstance(entry, AppConfig):
app_config = entry
else:
app_config = (entry)#创建⼀个AppConfig对象
if app_ in _configs:
raise ImproperlyConfigured(
"Application labels aren't unique, "
"duplicates: %s" % app_)
_configs[app_] = app_config
# Check for duplicate app names.
counts = Counter( #判断有⽆重复的AppConfig对象对象
app_ for app_config in _())
duplicates = [
name for name, count in _common() if count > 1]
if duplicates:
raise ImproperlyConfigured(
"Application names aren't unique, "
"duplicates: %s" % ", ".join(duplicates))
_ready = True
# Load models.
for app_config in _():#为每个app导⼊models_module
all_models = _models[app_]
app__models(all_models)
_cache()
_ready = True
for app_config in _app_configs():
app_()
= True
#以为例说明create过程
default_app_config = 'onfig'
class SimpleAdminConfig(AppConfig):
"""Simple AppConfig which does not do automatic discovery."""
name = '' #作为app_name
verbose_name = _("Administration")
def ready(self):
er(check_admin_app, )
class AdminConfig(SimpleAdminConfig):
"""The default AppConfig for admin which does autodiscovery."""
def ready(self):
super(AdminConfig, self).ready()
scover()
@classmethod #AppConfig类的创建⽅法
def create(cls, entry):
"""
Factory that creates an app config from an entry in INSTALLED_APPS.
"""
try:
# If import_module succeeds, entry is a path to an app module,
# which may specify an app config class with default_app_config.
# Otherwise, entry is a path to an app config class or an error.
module = import_module(entry)#会导⼊包的__init__.py
except ImportError:
# Track that importing as an app module failed. If importing as an
# app config class fails too, we'll trigger the ImportError again.
module = None
mod_path, _, cls_name = tion('.')
# Raise the original exception when entry cannot be a path to an
# app config class.
if not mod_path:
raise
else:
try:
# If this works, the app module specifies an app config class.
entry = t_app_config#从模块对象中取得默认配置⽂件类
except AttributeError:
# Otherwise, it simply uses the default app config class.
return cls(entry, module)
else:
mod_path, _, cls_name = tion('.')#分解成路径和类名
# If we're reaching this point, we must attempt to load the app config
# class located at
mod = import_module(mod_path)#导⼊apps模块
try:
cls = getattr(mod, cls_name)#取得类对象,⽐如在admin中为AdminConfig
except AttributeError:
if module is None:
# If importing as an app module failed, that error probably
# contains the most informative traceback. Trigger it again.
import_module(entry)
else:
raise
# Check for obvious errors. (This check prevents duck typing, but
# it could be removed if it became a problem in practice.)
if not issubclass(cls, AppConfig):
raise ImproperlyConfigured(
"'%s' isn't a subclass of AppConfig." % entry)
# Obtain app name here rather than in AppClass.__init__ to keep
# all error checking for entries in INSTALLED_APPS in one place.


发布评论