About Pretty Printing to Stdout

In your Django management commands, you can pprint an object to stdout by passing stdout._out as the stream parameter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# core/management/commands/pprint_setting.py
from pprint import pprint
from django.core.management.base import BaseCommand
from django.conf import settings

class Command(BaseCommand):
    help = "Prints out the requested setting"

    def add_arguments(self, parser):
        parser.add_argument("setting_name")

    def handle(self, *args, **options):
        setting_name = options.get("setting_name")
        setting_value = getattr(settings, setting_name)
        pprint(setting_value, stream=self.stdout._out)

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2 Python 3