diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index 1a166c6f99..51aaaef9c4 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -65,23 +65,14 @@ class Command(BaseCommand): # Make sure the app they asked for exists app_labels = set(app_labels) - bad_app_labels = set() + has_bad_labels = False for app_label in app_labels: try: apps.get_app_config(app_label) - except LookupError: - bad_app_labels.add(app_label) - if bad_app_labels: - for app_label in bad_app_labels: - if '.' in app_label: - self.stderr.write( - "'%s' is not a valid app label. Did you mean '%s'?" % ( - app_label, - app_label.split('.')[-1], - ) - ) - else: - self.stderr.write("App '%s' could not be found. Is it in INSTALLED_APPS?" % app_label) + except LookupError as err: + self.stderr.write(str(err)) + has_bad_labels = True + if has_bad_labels: sys.exit(2) # Load the current graph state. Pass in None for the connection so diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index a1488dc7b8..c9791af51e 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -1408,20 +1408,20 @@ class AppLabelErrorTests(TestCase): app. 'django.contrib.auth' must be in INSTALLED_APPS for some of these tests. """ + nonexistent_app_error = "No installed app with label 'nonexistent_app'." + did_you_mean_auth_error = ( + "No installed app with label 'django.contrib.auth'. Did you mean " + "'auth'?" + ) + def test_makemigrations_nonexistent_app_label(self): err = io.StringIO() with self.assertRaises(SystemExit): call_command('makemigrations', 'nonexistent_app', stderr=err) - self.assertIn( - "App 'nonexistent_app' could not be found. Is it in " - "INSTALLED_APPS?", err.getvalue() - ) + self.assertIn(self.nonexistent_app_error, err.getvalue()) def test_makemigrations_app_name_specified_as_label(self): err = io.StringIO() with self.assertRaises(SystemExit): call_command('makemigrations', 'django.contrib.auth', stderr=err) - self.assertIn( - "'django.contrib.auth' is not a valid app label. Did you mean 'auth'?", - err.getvalue() - ) + self.assertIn(self.did_you_mean_auth_error, err.getvalue())