The Setup
For the purpose of illustrating the problem, I have created these commands in my project:
foo.py
:
from django.core.management.base import BaseCommand
from django.core.management import call_command
class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write("foo")
# I pass `self.stdout` here explicitly because if `foo` is
# redirected, I want `baz` redirected too.
call_command('baz', stdout=self.stdout)
baz.py
:
from django.core.management.base import BaseCommand
from django.core.management import call_command
class Command(BaseCommand):
def handle(self, *args, **options):
# This could be reduced to one call to self.stdout.write
# but this code is meant to minimally reproduce what happens in a
# complex command where multiple self.stdout.write calls are
# made. If the code here were replaced with a single call, it
# would cease to reproduce the issue.
self.stdout.write("baz ", ending='')
# Imagine a lot of stuff happening here with conditionals and
# loops.
self.stdout.write("baz")
Actual Behavior
I run foo
like this:
./manage.py foo
And I get this output to the console:
foo
baz
baz
Desired Behavior
What I want is the output to the console to be:
foo
baz baz
Note that when I invoke baz
directly with ./manage.py baz
, I get this output:
baz baz
There is no newline between the two "baz". I want the same layout when baz
is invoked through foo
.