Django manage.py scripts¶
Creating your custom commands¶
- Create a file called:
app_name/management/commands/create_admin_superuser.py - Fill in the blanks
Example¶
from django.core.management.base 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¶
manage.py shell vs python console
manage.py shellsets theDJANGO_SETTINGS_MODULEwhich lets it know about thesettings.py
(django_extensions has a shell_plus)
django_extensions¶
adds a bunch of nice helpers to ./manage.py
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