From aaf5b3e7aad6fc8250143655854cc03c3b0a68c8 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 2 Jan 2014 20:43:45 +0100 Subject: [PATCH] Moved django.setup() to ManagementUtility In get_commands, setup() might already have been called, for example when the management command is called through call_command. Moving setup() to ManagementUtility so as it is only called when the command is run from command line. --- django/core/management/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index 5966d8929e..34597bfa5f 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -115,10 +115,6 @@ def get_commands(): # settings, like startproject or help. app_names = [] else: - # Setup Django outside of the try/except block to avoid catching - # ImproperlyConfigured errors that aren't caused by the absence of - # a settings module. - django.setup() app_configs = apps.get_app_configs() app_names = [app_config.name for app_config in app_configs] @@ -389,6 +385,12 @@ class ManagementUtility(object): except: # Needed because parser.parse_args can raise SystemExit pass # Ignore any option errors at this point. + try: + django.setup() + except ImproperlyConfigured: + # Some commands are supposed to work without configured settings + pass + try: subcommand = self.argv[1] except IndexError: