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.
This commit is contained in:
Claude Paroz 2014-01-02 20:43:45 +01:00
parent 7f110e7959
commit aaf5b3e7aa
1 changed files with 6 additions and 4 deletions

View File

@ -115,10 +115,6 @@ def get_commands():
# settings, like startproject or help. # settings, like startproject or help.
app_names = [] app_names = []
else: 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_configs = apps.get_app_configs()
app_names = [app_config.name for app_config in 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 except: # Needed because parser.parse_args can raise SystemExit
pass # Ignore any option errors at this point. pass # Ignore any option errors at this point.
try:
django.setup()
except ImproperlyConfigured:
# Some commands are supposed to work without configured settings
pass
try: try:
subcommand = self.argv[1] subcommand = self.argv[1]
except IndexError: except IndexError: