Skip to content

Django scripts

Write your own commands

Creating your custom commands

  1. Create a file called: app_name/management/commands/
  2. Fill in the blanks


from import BaseCommand

class Command(BaseCommand):
    help = """ help string """

    def add_arguments(self, parser):
        parser.add_argument("email", type=str)
        parser.add_argument("password", type=str)

    def handle(self, *args, **options):
        # do stuff
        self.stdout.write("Created an admin superuser")

Python Shell

python shell
import django
from django.contrib.auth.models import User, Group
# other imports shell vs python console

  • shell sets the DJANGO_SETTINGS_MODULEwhich lets it know about the

(django_extensions has a shell_plus)



adds a bunch of nice helpers to ./

  • graph_models
    • graphs your models
  • runserver_plus
    • django error page with a Python terminal to see the info
  • runscript
    • run a python file that sets up django (import django, django.setup())
    • nice for scripts that are run with cron
  • shell_plus
    • django shell that loads all of your models
  • print_user_for_session <session_ID>
    • get all of the session info for a user

Last update: 2023-04-24