Fixed #32285 -- Raised ImproperlyConfigured when AppConfig.label is not a valid Python identifier.

This commit is contained in:
Hasan Ramezani 2020-12-21 14:21:25 +01:00 committed by Mariusz Felisiak
parent 110001d0bb
commit 8b2a30f6f1
2 changed files with 12 additions and 0 deletions

View File

@ -34,6 +34,10 @@ class AppConfig:
# This value must be unique across a Django project.
if not hasattr(self, 'label'):
self.label = app_name.rpartition(".")[2]
if not self.label.isidentifier():
raise ImproperlyConfigured(
"The app label '%s' is not a valid Python identifier." % self.label
)
# Human-readable name for the application e.g. "Admin".
if not hasattr(self, 'verbose_name'):

View File

@ -436,6 +436,14 @@ class AppConfigTests(SimpleTestCase):
ac = AppConfig('label', Stub(__path__=['a']))
self.assertEqual(repr(ac), '<AppConfig: label>')
def test_invalid_label(self):
class MyAppConfig(AppConfig):
label = 'invalid.label'
msg = "The app label 'invalid.label' is not a valid Python identifier."
with self.assertRaisesMessage(ImproperlyConfigured, msg):
MyAppConfig('test_app', Stub())
@override_settings(
INSTALLED_APPS=['apps.apps.ModelPKAppsConfig'],
DEFAULT_AUTO_FIELD='django.db.models.SmallAutoField',