2020-07-21 16:35:12 +08:00
|
|
|
import inspect
|
2014-01-28 04:28:53 +08:00
|
|
|
import os
|
2020-07-21 16:35:12 +08:00
|
|
|
import warnings
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2013-12-13 04:34:39 +08:00
|
|
|
|
2016-10-01 03:08:35 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2020-07-21 16:35:12 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango41Warning
|
2020-07-12 20:59:57 +08:00
|
|
|
from django.utils.functional import cached_property
|
2020-07-21 16:35:12 +08:00
|
|
|
from django.utils.module_loading import import_string, module_has_submodule
|
2013-12-13 04:34:39 +08:00
|
|
|
|
2020-07-21 16:35:12 +08:00
|
|
|
APPS_MODULE_NAME = 'apps'
|
2013-12-18 18:28:21 +08:00
|
|
|
MODELS_MODULE_NAME = 'models'
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class AppConfig:
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Class representing a Django application and its configuration."""
|
2013-12-12 18:28:04 +08:00
|
|
|
|
2013-12-27 01:40:28 +08:00
|
|
|
def __init__(self, app_name, app_module):
|
2017-01-25 04:37:33 +08:00
|
|
|
# Full Python path to the application e.g. 'django.contrib.admin'.
|
2013-12-18 18:28:21 +08:00
|
|
|
self.name = app_name
|
2013-12-13 06:33:09 +08:00
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
# Root module for the application e.g. <module 'django.contrib.admin'
|
2017-01-22 09:02:00 +08:00
|
|
|
# from 'django/contrib/admin/__init__.py'>.
|
2013-12-27 01:40:28 +08:00
|
|
|
self.module = app_module
|
2013-12-20 21:03:14 +08:00
|
|
|
|
2016-10-01 03:08:35 +08:00
|
|
|
# Reference to the Apps registry that holds this AppConfig. Set by the
|
|
|
|
# registry when it registers the AppConfig instance.
|
|
|
|
self.apps = None
|
|
|
|
|
2013-12-20 21:03:14 +08:00
|
|
|
# The following attributes could be defined at the class level in a
|
|
|
|
# subclass, hence the test-and-set pattern.
|
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
# Last component of the Python path to the application e.g. 'admin'.
|
2013-12-13 06:33:09 +08:00
|
|
|
# This value must be unique across a Django project.
|
2013-12-20 21:03:14 +08:00
|
|
|
if not hasattr(self, 'label'):
|
|
|
|
self.label = app_name.rpartition(".")[2]
|
2020-12-21 21:21:25 +08:00
|
|
|
if not self.label.isidentifier():
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The app label '%s' is not a valid Python identifier." % self.label
|
|
|
|
)
|
2013-12-13 06:33:09 +08:00
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
# Human-readable name for the application e.g. "Admin".
|
2013-12-20 21:03:14 +08:00
|
|
|
if not hasattr(self, 'verbose_name'):
|
|
|
|
self.verbose_name = self.label.title()
|
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
# Filesystem path to the application directory e.g.
|
2017-01-22 09:02:00 +08:00
|
|
|
# '/path/to/django/contrib/admin'.
|
2013-12-20 21:03:14 +08:00
|
|
|
if not hasattr(self, 'path'):
|
2014-01-28 04:28:53 +08:00
|
|
|
self.path = self._path_from_module(app_module)
|
2013-12-13 04:34:39 +08:00
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
# Module containing models e.g. <module 'django.contrib.admin.models'
|
2017-01-22 09:02:00 +08:00
|
|
|
# from 'django/contrib/admin/models.py'>. Set by import_models().
|
2013-12-18 18:28:21 +08:00
|
|
|
# None if the application doesn't have a models module.
|
|
|
|
self.models_module = None
|
2013-12-12 18:28:04 +08:00
|
|
|
|
2018-09-25 22:30:18 +08:00
|
|
|
# Mapping of lowercase model names to model classes. Initially set to
|
2013-12-18 18:28:21 +08:00
|
|
|
# None to prevent accidental access before import_models() runs.
|
|
|
|
self.models = None
|
2013-12-13 04:34:39 +08:00
|
|
|
|
2013-12-12 18:28:04 +08:00
|
|
|
def __repr__(self):
|
2013-12-29 03:13:08 +08:00
|
|
|
return '<%s: %s>' % (self.__class__.__name__, self.label)
|
2013-12-18 18:28:21 +08:00
|
|
|
|
2020-07-12 20:59:57 +08:00
|
|
|
@cached_property
|
|
|
|
def default_auto_field(self):
|
|
|
|
from django.conf import settings
|
|
|
|
return settings.DEFAULT_AUTO_FIELD
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _is_default_auto_field_overridden(self):
|
|
|
|
return self.__class__.default_auto_field is not AppConfig.default_auto_field
|
|
|
|
|
2014-01-28 04:28:53 +08:00
|
|
|
def _path_from_module(self, module):
|
|
|
|
"""Attempt to determine app's filesystem path from its module."""
|
|
|
|
# See #21874 for extended discussion of the behavior of this method in
|
|
|
|
# various cases.
|
2021-03-26 17:14:07 +08:00
|
|
|
# Convert to list because __path__ may not support indexing.
|
2014-01-28 04:28:53 +08:00
|
|
|
paths = list(getattr(module, '__path__', []))
|
|
|
|
if len(paths) != 1:
|
|
|
|
filename = getattr(module, '__file__', None)
|
|
|
|
if filename is not None:
|
|
|
|
paths = [os.path.dirname(filename)]
|
2015-08-24 08:07:00 +08:00
|
|
|
else:
|
|
|
|
# For unknown reasons, sometimes the list returned by __path__
|
|
|
|
# contains duplicates that must be removed (#25246).
|
|
|
|
paths = list(set(paths))
|
2014-01-28 04:28:53 +08:00
|
|
|
if len(paths) > 1:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The app module %r has multiple filesystem locations (%r); "
|
|
|
|
"you must configure this app with an AppConfig subclass "
|
|
|
|
"with a 'path' class attribute." % (module, paths))
|
|
|
|
elif not paths:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The app module %r has no filesystem location, "
|
|
|
|
"you must configure this app with an AppConfig subclass "
|
2020-04-25 21:53:30 +08:00
|
|
|
"with a 'path' class attribute." % module)
|
2017-01-20 21:01:02 +08:00
|
|
|
return paths[0]
|
2014-01-28 04:28:53 +08:00
|
|
|
|
2013-12-20 21:03:14 +08:00
|
|
|
@classmethod
|
|
|
|
def create(cls, entry):
|
|
|
|
"""
|
|
|
|
Factory that creates an app config from an entry in INSTALLED_APPS.
|
|
|
|
"""
|
2020-07-21 16:35:12 +08:00
|
|
|
# create() eventually returns app_config_class(app_name, app_module).
|
|
|
|
app_config_class = None
|
2020-08-11 02:16:45 +08:00
|
|
|
app_config_name = None
|
2020-07-21 16:35:12 +08:00
|
|
|
app_name = None
|
|
|
|
app_module = None
|
2013-12-20 21:03:14 +08:00
|
|
|
|
2020-07-21 16:35:12 +08:00
|
|
|
# If import_module succeeds, entry points to the app module.
|
|
|
|
try:
|
|
|
|
app_module = import_module(entry)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2014-01-25 05:43:00 +08:00
|
|
|
else:
|
2020-07-21 16:35:12 +08:00
|
|
|
# If app_module has an apps submodule that defines a single
|
|
|
|
# AppConfig subclass, use it automatically.
|
|
|
|
# To prevent this, an AppConfig subclass can declare a class
|
|
|
|
# variable default = False.
|
|
|
|
# If the apps module defines more than one AppConfig subclass,
|
|
|
|
# the default one can declare default = True.
|
|
|
|
if module_has_submodule(app_module, APPS_MODULE_NAME):
|
|
|
|
mod_path = '%s.%s' % (entry, APPS_MODULE_NAME)
|
|
|
|
mod = import_module(mod_path)
|
|
|
|
# Check if there's exactly one AppConfig candidate,
|
|
|
|
# excluding those that explicitly define default = False.
|
|
|
|
app_configs = [
|
|
|
|
(name, candidate)
|
|
|
|
for name, candidate in inspect.getmembers(mod, inspect.isclass)
|
|
|
|
if (
|
|
|
|
issubclass(candidate, cls) and
|
|
|
|
candidate is not cls and
|
|
|
|
getattr(candidate, 'default', True)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
if len(app_configs) == 1:
|
|
|
|
app_config_class = app_configs[0][1]
|
|
|
|
app_config_name = '%s.%s' % (mod_path, app_configs[0][0])
|
|
|
|
else:
|
|
|
|
# Check if there's exactly one AppConfig subclass,
|
|
|
|
# among those that explicitly define default = True.
|
|
|
|
app_configs = [
|
|
|
|
(name, candidate)
|
|
|
|
for name, candidate in app_configs
|
|
|
|
if getattr(candidate, 'default', False)
|
|
|
|
]
|
|
|
|
if len(app_configs) > 1:
|
|
|
|
candidates = [repr(name) for name, _ in app_configs]
|
|
|
|
raise RuntimeError(
|
|
|
|
'%r declares more than one default AppConfig: '
|
|
|
|
'%s.' % (mod_path, ', '.join(candidates))
|
|
|
|
)
|
|
|
|
elif len(app_configs) == 1:
|
|
|
|
app_config_class = app_configs[0][1]
|
|
|
|
app_config_name = '%s.%s' % (mod_path, app_configs[0][0])
|
|
|
|
|
|
|
|
# If app_module specifies a default_app_config, follow the link.
|
|
|
|
# default_app_config is deprecated, but still takes over the
|
|
|
|
# automatic detection for backwards compatibility during the
|
|
|
|
# deprecation period.
|
2013-12-20 21:03:14 +08:00
|
|
|
try:
|
2020-07-21 16:35:12 +08:00
|
|
|
new_entry = app_module.default_app_config
|
2013-12-20 21:03:14 +08:00
|
|
|
except AttributeError:
|
2020-07-21 16:35:12 +08:00
|
|
|
# Use the default app config class if we didn't find anything.
|
|
|
|
if app_config_class is None:
|
|
|
|
app_config_class = cls
|
|
|
|
app_name = entry
|
2014-01-25 05:43:00 +08:00
|
|
|
else:
|
2020-07-21 16:35:12 +08:00
|
|
|
message = (
|
|
|
|
'%r defines default_app_config = %r. ' % (entry, new_entry)
|
2018-09-19 00:19:18 +08:00
|
|
|
)
|
2020-07-21 16:35:12 +08:00
|
|
|
if new_entry == app_config_name:
|
|
|
|
message += (
|
|
|
|
'Django now detects this configuration automatically. '
|
|
|
|
'You can remove default_app_config.'
|
2018-09-19 00:19:18 +08:00
|
|
|
)
|
2020-07-21 16:35:12 +08:00
|
|
|
else:
|
|
|
|
message += (
|
2020-08-11 02:16:45 +08:00
|
|
|
"However, Django's automatic detection %s. You should "
|
|
|
|
"move the default config class to the apps submodule "
|
|
|
|
"of your application and, if this module defines "
|
|
|
|
"several config classes, mark the default one with "
|
|
|
|
"default = True." % (
|
|
|
|
"picked another configuration, %r" % app_config_name
|
|
|
|
if app_config_name
|
|
|
|
else "did not find this configuration"
|
|
|
|
)
|
2020-07-21 16:35:12 +08:00
|
|
|
)
|
|
|
|
warnings.warn(message, RemovedInDjango41Warning, stacklevel=2)
|
|
|
|
entry = new_entry
|
|
|
|
app_config_class = None
|
|
|
|
|
|
|
|
# If import_string succeeds, entry is an app config class.
|
|
|
|
if app_config_class is None:
|
|
|
|
try:
|
|
|
|
app_config_class = import_string(entry)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
# If both import_module and import_string failed, it means that entry
|
|
|
|
# doesn't have a valid value.
|
|
|
|
if app_module is None and app_config_class is None:
|
|
|
|
# If the last component of entry starts with an uppercase letter,
|
|
|
|
# then it was likely intended to be an app config class; if not,
|
|
|
|
# an app module. Provide a nice error message in both cases.
|
|
|
|
mod_path, _, cls_name = entry.rpartition('.')
|
|
|
|
if mod_path and cls_name[0].isupper():
|
|
|
|
# We could simply re-trigger the string import exception, but
|
|
|
|
# we're going the extra mile and providing a better error
|
|
|
|
# message for typos in INSTALLED_APPS.
|
|
|
|
# This may raise ImportError, which is the best exception
|
|
|
|
# possible if the module at mod_path cannot be imported.
|
|
|
|
mod = import_module(mod_path)
|
|
|
|
candidates = [
|
|
|
|
repr(name)
|
|
|
|
for name, candidate in inspect.getmembers(mod, inspect.isclass)
|
|
|
|
if issubclass(candidate, cls) and candidate is not cls
|
|
|
|
]
|
|
|
|
msg = "Module '%s' does not contain a '%s' class." % (mod_path, cls_name)
|
|
|
|
if candidates:
|
|
|
|
msg += ' Choices are: %s.' % ', '.join(candidates)
|
|
|
|
raise ImportError(msg)
|
2014-09-01 00:51:55 +08:00
|
|
|
else:
|
2020-07-21 16:35:12 +08:00
|
|
|
# Re-trigger the module import exception.
|
|
|
|
import_module(entry)
|
2014-01-25 05:43:00 +08:00
|
|
|
|
|
|
|
# Check for obvious errors. (This check prevents duck typing, but
|
|
|
|
# it could be removed if it became a problem in practice.)
|
2020-07-21 16:35:12 +08:00
|
|
|
if not issubclass(app_config_class, AppConfig):
|
2014-01-25 05:43:00 +08:00
|
|
|
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.
|
2020-07-21 16:35:12 +08:00
|
|
|
if app_name is None:
|
|
|
|
try:
|
|
|
|
app_name = app_config_class.name
|
|
|
|
except AttributeError:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"'%s' must supply a name attribute." % entry
|
|
|
|
)
|
2013-12-27 01:40:28 +08:00
|
|
|
|
2014-01-25 05:43:00 +08:00
|
|
|
# Ensure app_name points to a valid module.
|
2016-06-02 04:08:59 +08:00
|
|
|
try:
|
|
|
|
app_module = import_module(app_name)
|
|
|
|
except ImportError:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Cannot import '%s'. Check that '%s.%s.name' is correct." % (
|
2020-07-21 16:35:12 +08:00
|
|
|
app_name,
|
|
|
|
app_config_class.__module__,
|
|
|
|
app_config_class.__qualname__,
|
2016-06-02 04:08:59 +08:00
|
|
|
)
|
|
|
|
)
|
2014-01-25 05:43:00 +08:00
|
|
|
|
|
|
|
# Entry is a path to an app config class.
|
2020-07-21 16:35:12 +08:00
|
|
|
return app_config_class(app_name, app_module)
|
2013-12-20 21:03:14 +08:00
|
|
|
|
2016-05-03 18:21:54 +08:00
|
|
|
def get_model(self, model_name, require_ready=True):
|
2013-12-28 21:41:11 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Return the model with the given case-insensitive model_name.
|
2013-12-28 21:41:11 +08:00
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
Raise LookupError if no model exists with this name.
|
2013-12-28 21:41:11 +08:00
|
|
|
"""
|
2016-05-03 18:21:54 +08:00
|
|
|
if require_ready:
|
|
|
|
self.apps.check_models_ready()
|
|
|
|
else:
|
|
|
|
self.apps.check_apps_ready()
|
2013-12-28 21:41:11 +08:00
|
|
|
try:
|
|
|
|
return self.models[model_name.lower()]
|
|
|
|
except KeyError:
|
|
|
|
raise LookupError(
|
|
|
|
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
|
|
|
|
|
2016-02-02 17:33:09 +08:00
|
|
|
def get_models(self, include_auto_created=False, include_swapped=False):
|
2013-12-30 03:26:13 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Return an iterable of models.
|
2013-12-30 03:26:13 +08:00
|
|
|
|
|
|
|
By default, the following models aren't included:
|
|
|
|
|
|
|
|
- auto-created models for many-to-many relations without
|
|
|
|
an explicit intermediate table,
|
|
|
|
- models that have been swapped out.
|
|
|
|
|
|
|
|
Set the corresponding keyword argument to True to include such models.
|
|
|
|
Keyword arguments aren't documented; they're a private API.
|
|
|
|
"""
|
2016-10-01 03:08:35 +08:00
|
|
|
self.apps.check_models_ready()
|
2013-12-30 03:26:13 +08:00
|
|
|
for model in self.models.values():
|
|
|
|
if model._meta.auto_created and not include_auto_created:
|
|
|
|
continue
|
|
|
|
if model._meta.swapped and not include_swapped:
|
|
|
|
continue
|
|
|
|
yield model
|
|
|
|
|
2016-10-01 03:35:04 +08:00
|
|
|
def import_models(self):
|
2013-12-26 21:12:30 +08:00
|
|
|
# Dictionary of models for this app, primarily maintained in the
|
|
|
|
# 'all_models' attribute of the Apps this AppConfig is attached to.
|
2016-10-01 03:35:04 +08:00
|
|
|
self.models = self.apps.all_models[self.label]
|
2013-12-18 18:28:21 +08:00
|
|
|
|
2013-12-27 01:40:28 +08:00
|
|
|
if module_has_submodule(self.module, MODELS_MODULE_NAME):
|
2013-12-18 18:28:21 +08:00
|
|
|
models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
|
|
|
|
self.models_module = import_module(models_module_name)
|
2013-12-30 19:49:53 +08:00
|
|
|
|
2014-01-01 00:55:12 +08:00
|
|
|
def ready(self):
|
2013-12-30 19:49:53 +08:00
|
|
|
"""
|
2014-01-01 00:55:12 +08:00
|
|
|
Override this method in subclasses to run code when Django starts.
|
2013-12-30 19:49:53 +08:00
|
|
|
"""
|