summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2016-03-02 17:12:56 -0800
committerTim Graham <timograham@gmail.com>2016-03-05 13:05:10 -0500
commit4115288b4f7bbd694946a1ddef0f0ba85c03f9a1 (patch)
tree829552eea236ba5cf7cccdb873e014bfff6a6036 /django
parent8d3fcfa39e8aab5618d9b7f6a592006e9af8cefc (diff)
Fixed #26315 -- Allowed call_command() to accept a Command object as the first argument.
Diffstat (limited to 'django')
-rw-r--r--django/core/management/__init__.py33
1 files changed, 23 insertions, 10 deletions
diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
index f9e11b48a6..2495d38642 100644
--- a/django/core/management/__init__.py
+++ b/django/core/management/__init__.py
@@ -82,22 +82,35 @@ def call_command(name, *args, **options):
This is the primary API you should use for calling specific commands.
+ `name` may be a string or a command object. Using a string is preferred
+ unless the command object is required for further processing or testing.
+
Some examples:
call_command('migrate')
call_command('shell', plain=True)
call_command('sqlmigrate', 'myapp')
- """
- # Load the command object.
- try:
- app_name = get_commands()[name]
- except KeyError:
- raise CommandError("Unknown command: %r" % name)
- if isinstance(app_name, BaseCommand):
- # If the command is already loaded, use it directly.
- command = app_name
+ from django.core.management.commands import flush
+ cmd = flush.Command()
+ call_command(cmd, verbosity=0, interactive=False)
+ # Do something with cmd ...
+ """
+ if isinstance(name, BaseCommand):
+ # Command object passed in.
+ command = name
+ name = command.__class__.__module__.split('.')[-1]
else:
- command = load_command_class(app_name, name)
+ # Load the command object by name.
+ try:
+ app_name = get_commands()[name]
+ except KeyError:
+ raise CommandError("Unknown command: %r" % name)
+
+ if isinstance(app_name, BaseCommand):
+ # If the command is already loaded, use it directly.
+ command = app_name
+ else:
+ command = load_command_class(app_name, name)
# Simulate argument parsing to get the option defaults (see #10080 for details).
parser = command.create_parser('', name)