diff --git a/django/core/management/base.py b/django/core/management/base.py index ce9ba0be48c..9434691ccbc 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -348,18 +348,17 @@ class BaseCommand(object): output = self.handle(*args, **options) if output: if self.output_transaction: - # This needs to be imported here, because it relies on - # settings. - from django.db import connections, DEFAULT_DB_ALIAS connection = connections[options.get('database', DEFAULT_DB_ALIAS)] - if connection.ops.start_transaction_sql(): - self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql())) + output = '%s\n%s\n%s' % ( + self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()), + output, + self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()), + ) self.stdout.write(output) - if self.output_transaction: - self.stdout.write('\n' + self.style.SQL_KEYWORD(connection.ops.end_transaction_sql())) finally: if saved_locale is not None: translation.activate(saved_locale) + return output def check(self, app_configs=None, tags=None, display_num_errors=False, include_deployment_checks=False, fail_level=checks.ERROR): diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 87753b28bd8..6a7752f40cd 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -1793,6 +1793,14 @@ Command options which take multiple options are passed a list:: management.call_command('dumpdata', exclude=['contenttypes', 'auth']) +The return value of the ``call_command()`` function is the same as the return +value of the ``handle()`` method of the command. + +.. versionchanged:: 1.10 + + ``call_command()`` now returns the value received from the + ``command.handle()`` method. + Output redirection ================== diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index db6b730670c..658a3646bd3 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -230,6 +230,9 @@ Internationalization Management Commands ~~~~~~~~~~~~~~~~~~~ +* :func:`~django.core.management.call_command` now returns the value returned + from the ``command.handle()`` method. + * The new :option:`check --fail-level` option allows specifying the message level that will cause the command to exit with a non-zero status. diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index 9c27fb66692..f4e6c663534 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -61,17 +61,15 @@ class CommandTests(SimpleTestCase): def test_deactivate_locale_set(self): # Deactivate translation when set to true - out = StringIO() with translation.override('pl'): - management.call_command('leave_locale_alone_false', stdout=out) - self.assertEqual(out.getvalue(), "") + result = management.call_command('leave_locale_alone_false', stdout=StringIO()) + self.assertIsNone(result) def test_configured_locale_preserved(self): # Leaves locale from settings when set to false - out = StringIO() with translation.override('pl'): - management.call_command('leave_locale_alone_true', stdout=out) - self.assertEqual(out.getvalue(), "pl\n") + result = management.call_command('leave_locale_alone_true', stdout=StringIO()) + self.assertEqual(result, "pl") def test_find_command_without_PATH(self): """ @@ -132,16 +130,13 @@ class CommandTests(SimpleTestCase): self.assertIn("Dave, my mind is going. I can feel it. I can feel it.\n", out.getvalue()) def test_calling_a_command_with_no_app_labels_and_parameters_should_raise_a_command_error(self): - out = StringIO() with self.assertRaises(CommandError): - management.call_command('hal', stdout=out) + management.call_command('hal', stdout=StringIO()) def test_output_transaction(self): - out = StringIO() - management.call_command('transaction', stdout=out, no_color=True) - output = out.getvalue().strip() - self.assertTrue(output.startswith(connection.ops.start_transaction_sql())) - self.assertTrue(output.endswith(connection.ops.end_transaction_sql())) + output = management.call_command('transaction', stdout=StringIO(), no_color=True) + self.assertTrue(output.strip().startswith(connection.ops.start_transaction_sql())) + self.assertTrue(output.strip().endswith(connection.ops.end_transaction_sql())) def test_call_command_no_checks(self): """