Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26190 closed New feature (fixed)

Return final management command result from call_command

Reported by: Claude Paroz Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, the result returned from the handle method of a management command is directed by default to the console in the execute method by writing to self.stdout, but it is not returned from the execute method, so call_command does not receive it either.

I would suggest to adapt this behavior and also return this output from execute (and consequently by call_command so we can easily get access to this result from call_command). The behavior when calling the command by the command line would be unchanged.

I even thought about not writing this result to self.stdout when the command is called by call_command, but this would be backwards incompatible and I don't see a good deprecation path for this.

Change History (7)

comment:1 by Tim Graham, 8 years ago

Would this be an alternative to the currently documented method of:

with open('/path/to/command_output') as f:
    management.call_command('dumpdata', stdout=f)

or is there some other difference?

comment:2 by Claude Paroz, 8 years ago

Only the return value of the command handle() method would be returned from call_command.

In the current Django commands, only the following are returning some content: diffsettings, sqlflush, sqlmigrate, sqlsequencereset, changepassword (success message), findstatic. Other commands are calling self.stdout.write during the processing of the command.

To get all the content output by a command, you still have to provide a catcher in the sdtout argument as you mentioned.

comment:3 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

I guess I don't have any objections other than I always found the return value of handle() to be somewhat inconsistent and ill-defined as to when that should be preferred instead of having the handle() method itself do the logging.

comment:4 by Claude Paroz, 8 years ago

Has patch: set

comment:5 by Tim Graham, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Claude Paroz <claude@…>, 8 years ago

Resolution: fixed
Status: newclosed

In b46c0ea6:

Fixed #26190 -- Returned handle() result from call_command

Thanks Tim Graham for the review.

comment:7 by Claude Paroz <claude@…>, 8 years ago

In 269b5f2:

Used call_command return value in staticfiles tests

Refs #26190.

Note: See TracTickets for help on using tickets.
Back to Top