设置
为了说明问题,我在我的项目中创建了这些命令:
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")
实际行为
我foo
这样跑:
./manage.py foo
我得到这个输出到控制台:
foo
baz
baz
期望的行为
我想要的是控制台的输出是:
foo
baz baz
请注意,当我baz
直接调用 with 时./manage.py baz
,会得到以下输出:
baz baz
两个“baz”之间没有换行符。baz
通过调用时,我想要相同的布局foo
。