Fixed #22699 -- Configure default settings in some management commands.
This makes it possible to run django.setup() in management commands that don't need a settings module. In addition it simplifies error handling. Thanks Claude for the review.
This commit is contained in:
parent
2e613ea5c5
commit
4865326f22
|
@ -107,18 +107,11 @@ def get_commands():
|
||||||
"""
|
"""
|
||||||
commands = {name: 'django.core' for name in find_commands(__path__[0])}
|
commands = {name: 'django.core' for name in find_commands(__path__[0])}
|
||||||
|
|
||||||
# Find the installed apps
|
if not settings.configured:
|
||||||
try:
|
return commands
|
||||||
settings.INSTALLED_APPS
|
|
||||||
except ImproperlyConfigured:
|
app_names = [app_config.name for app_config in apps.get_app_configs()]
|
||||||
# Still useful for commands that do not require functional
|
|
||||||
# settings, like startproject or help.
|
|
||||||
app_names = []
|
|
||||||
else:
|
|
||||||
app_configs = apps.get_app_configs()
|
|
||||||
app_names = [app_config.name for app_config in app_configs]
|
|
||||||
|
|
||||||
# Find and load the management module for each installed app.
|
|
||||||
for app_name in reversed(app_names):
|
for app_name in reversed(app_names):
|
||||||
try:
|
try:
|
||||||
path = find_management_module(app_name)
|
path = find_management_module(app_name)
|
||||||
|
@ -232,6 +225,7 @@ class ManagementUtility(object):
|
||||||
def __init__(self, argv=None):
|
def __init__(self, argv=None):
|
||||||
self.argv = argv or sys.argv[:]
|
self.argv = argv or sys.argv[:]
|
||||||
self.prog_name = os.path.basename(self.argv[0])
|
self.prog_name = os.path.basename(self.argv[0])
|
||||||
|
self.settings_exception = None
|
||||||
|
|
||||||
def main_help_text(self, commands_only=False):
|
def main_help_text(self, commands_only=False):
|
||||||
"""
|
"""
|
||||||
|
@ -260,12 +254,11 @@ class ManagementUtility(object):
|
||||||
for name in sorted(commands_dict[app]):
|
for name in sorted(commands_dict[app]):
|
||||||
usage.append(" %s" % name)
|
usage.append(" %s" % name)
|
||||||
# Output an extra note if settings are not properly configured
|
# Output an extra note if settings are not properly configured
|
||||||
try:
|
if self.settings_exception is not None:
|
||||||
settings.INSTALLED_APPS
|
|
||||||
except ImproperlyConfigured as err:
|
|
||||||
usage.append(style.NOTICE(
|
usage.append(style.NOTICE(
|
||||||
"Note that only Django core commands are listed as settings "
|
"Note that only Django core commands are listed "
|
||||||
"are not properly configured (error: %s)." % err))
|
"as settings are not properly configured (error: %s)."
|
||||||
|
% self.settings_exception))
|
||||||
|
|
||||||
return '\n'.join(usage)
|
return '\n'.join(usage)
|
||||||
|
|
||||||
|
@ -384,21 +377,31 @@ 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:
|
|
||||||
settings.INSTALLED_APPS
|
|
||||||
except ImproperlyConfigured:
|
|
||||||
# Some commands are supposed to work without configured settings
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
django.setup()
|
|
||||||
|
|
||||||
self.autocomplete()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subcommand = self.argv[1]
|
subcommand = self.argv[1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
subcommand = 'help' # Display help if no arguments were given.
|
subcommand = 'help' # Display help if no arguments were given.
|
||||||
|
|
||||||
|
no_settings_commands = [
|
||||||
|
'help', 'version', '--help', '--version', '-h',
|
||||||
|
'compilemessages', 'makemessages',
|
||||||
|
'startapp', 'startproject',
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
settings.INSTALLED_APPS
|
||||||
|
except ImproperlyConfigured as exc:
|
||||||
|
self.settings_exception = exc
|
||||||
|
# A handful of built-in management commands work without settings.
|
||||||
|
# Load the default settings -- where INSTALLED_APPS is empty.
|
||||||
|
if subcommand in no_settings_commands:
|
||||||
|
settings.configure()
|
||||||
|
|
||||||
|
if settings.configured:
|
||||||
|
django.setup()
|
||||||
|
|
||||||
|
self.autocomplete()
|
||||||
|
|
||||||
if subcommand == 'help':
|
if subcommand == 'help':
|
||||||
if len(args) <= 2:
|
if len(args) <= 2:
|
||||||
parser.print_lax_help()
|
parser.print_lax_help()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
# Regression for #22699.
|
||||||
|
# Generated at {% now "DATE_FORMAT" %}
|
Loading…
Reference in New Issue