mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #31870 -- Fixed crash when populating app registry with empty or without apps module.
Regression in 3f2821af6b
.
This commit is contained in:
parent
ad827ddaef
commit
ebd78a9f97
@ -89,6 +89,7 @@ class AppConfig:
|
|||||||
"""
|
"""
|
||||||
# create() eventually returns app_config_class(app_name, app_module).
|
# create() eventually returns app_config_class(app_name, app_module).
|
||||||
app_config_class = None
|
app_config_class = None
|
||||||
|
app_config_name = None
|
||||||
app_name = None
|
app_name = None
|
||||||
app_module = None
|
app_module = None
|
||||||
|
|
||||||
@ -161,12 +162,15 @@ class AppConfig:
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
message += (
|
message += (
|
||||||
"However, Django's automatic detection picked another "
|
"However, Django's automatic detection %s. You should "
|
||||||
"configuration, %r. You should move the default "
|
"move the default config class to the apps submodule "
|
||||||
"config class to the apps submodule of your "
|
"of your application and, if this module defines "
|
||||||
"application and, if this module defines several "
|
"several config classes, mark the default one with "
|
||||||
"config classes, mark the default one with default = "
|
"default = True." % (
|
||||||
"True." % app_config_name
|
"picked another configuration, %r" % app_config_name
|
||||||
|
if app_config_name
|
||||||
|
else "did not find this configuration"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
warnings.warn(message, RemovedInDjango41Warning, stacklevel=2)
|
warnings.warn(message, RemovedInDjango41Warning, stacklevel=2)
|
||||||
entry = new_entry
|
entry = new_entry
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
default_app_config = 'apps.explicit_default_config_empty_apps.ExplicitDefaultConfigEmptyApps'
|
||||||
|
|
||||||
|
|
||||||
|
class ExplicitDefaultConfigEmptyApps(AppConfig):
|
||||||
|
name = 'apps.explicit_default_config_empty_apps'
|
@ -0,0 +1,7 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
default_app_config = 'apps.explicit_default_config_without_apps.ExplicitDefaultConfigWithoutApps'
|
||||||
|
|
||||||
|
|
||||||
|
class ExplicitDefaultConfigWithoutApps(AppConfig):
|
||||||
|
name = 'apps.explicit_default_config_without_apps'
|
@ -10,9 +10,13 @@ from django.test.utils import extend_sys_path, isolate_apps
|
|||||||
from django.utils.deprecation import RemovedInDjango41Warning
|
from django.utils.deprecation import RemovedInDjango41Warning
|
||||||
|
|
||||||
from .explicit_default_config_app.apps import ExplicitDefaultConfig
|
from .explicit_default_config_app.apps import ExplicitDefaultConfig
|
||||||
|
from .explicit_default_config_empty_apps import ExplicitDefaultConfigEmptyApps
|
||||||
from .explicit_default_config_mismatch_app.not_apps import (
|
from .explicit_default_config_mismatch_app.not_apps import (
|
||||||
ExplicitDefaultConfigMismatch,
|
ExplicitDefaultConfigMismatch,
|
||||||
)
|
)
|
||||||
|
from .explicit_default_config_without_apps import (
|
||||||
|
ExplicitDefaultConfigWithoutApps,
|
||||||
|
)
|
||||||
from .models import SoAlternative, TotallyNormal, new_apps
|
from .models import SoAlternative, TotallyNormal, new_apps
|
||||||
from .one_config_app.apps import OneConfig
|
from .one_config_app.apps import OneConfig
|
||||||
from .two_configs_one_default_app.apps import TwoConfig
|
from .two_configs_one_default_app.apps import TwoConfig
|
||||||
@ -520,3 +524,51 @@ class DeprecationTests(SimpleTestCase):
|
|||||||
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_mismatch_app']):
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_mismatch_app']):
|
||||||
config = apps.get_app_config('explicit_default_config_mismatch_app')
|
config = apps.get_app_config('explicit_default_config_mismatch_app')
|
||||||
self.assertIsInstance(config, ExplicitDefaultConfigMismatch)
|
self.assertIsInstance(config, ExplicitDefaultConfigMismatch)
|
||||||
|
|
||||||
|
def test_explicit_default_app_config_empty_apps(self):
|
||||||
|
"""
|
||||||
|
Load an app that specifies a default AppConfig class in __init__ and
|
||||||
|
have an empty apps module.
|
||||||
|
"""
|
||||||
|
msg = (
|
||||||
|
"'apps.explicit_default_config_empty_apps' defines "
|
||||||
|
"default_app_config = 'apps.explicit_default_config_empty_apps."
|
||||||
|
"ExplicitDefaultConfigEmptyApps'. However, Django's automatic "
|
||||||
|
"detection did not find this configuration. 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."
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
|
||||||
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_empty_apps']):
|
||||||
|
pass
|
||||||
|
with ignore_warnings(category=RemovedInDjango41Warning):
|
||||||
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_empty_apps']):
|
||||||
|
self.assertIsInstance(
|
||||||
|
apps.get_app_config('explicit_default_config_empty_apps'),
|
||||||
|
ExplicitDefaultConfigEmptyApps,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_explicit_default_app_config_without_apps(self):
|
||||||
|
"""
|
||||||
|
Load an app that specifies a default AppConfig class in __init__ and do
|
||||||
|
not have an apps module.
|
||||||
|
"""
|
||||||
|
msg = (
|
||||||
|
"'apps.explicit_default_config_without_apps' defines "
|
||||||
|
"default_app_config = 'apps.explicit_default_config_without_apps."
|
||||||
|
"ExplicitDefaultConfigWithoutApps'. However, Django's automatic "
|
||||||
|
"detection did not find this configuration. 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."
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(RemovedInDjango41Warning, msg):
|
||||||
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_without_apps']):
|
||||||
|
pass
|
||||||
|
with ignore_warnings(category=RemovedInDjango41Warning):
|
||||||
|
with self.settings(INSTALLED_APPS=['apps.explicit_default_config_without_apps']):
|
||||||
|
self.assertIsInstance(
|
||||||
|
apps.get_app_config('explicit_default_config_without_apps'),
|
||||||
|
ExplicitDefaultConfigWithoutApps,
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user