I've been trying to redirect the standard output of a custom django command using this piece of code:
from django.core.management.base import BaseCommand
from django.core import management
class Command(BaseCommand):
def handle(self, *args, **options):
f = open('/tmp/output', 'r+')
management.call_command('basequery', 'list', 'log', stdout=f)
f.close()
However, when I call this from manage.py the standard output appears on the console and the /tmp/output file is created but empty.
Here's the django documentation of what I'm trying to do
Your command is probably just using print directly. To be able to capture or redirect prints in a management command, you'll want to use the self.stdout handle of the command instance:
from __future__ import print_function
class Command(BaseCommand):
def handle(self, *args, **options):
# incorrect way to print in a management command:
print('This line will go to the terminal')
# correct ways to print in a management command:
print('This line will go into the StringIO', file=self.stdout)
self.stdout.write('This will also go into the StringIO')
If you're unable change the print calls within the command (which is the code within 'basequery' in your example), then you can use a context manager to temporarily redirect stdout in order to capture that output. It's important to restore the old stdout after redirection. See contextlib.redirect_stdout.
所有评论(0)