#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 , 10 years ago
comment:2 by , 10 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 , 10 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
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:5 by , 10 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
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?