It is possible to run custom code from django project from shell by passing it as an argument to manage.py
python manage.py CUSTOM_COMMAND
Use case example :
When we want to wait for database connection before running server
To do this,
1) Create the following folder structure inside your one of your application folder (say core)
core management __init__.py commands __init__.py CUSTOM_COMMAND.py
2) In CUSTOM_COMMAND.py, define a class named Command extending from django.core.management.base.BaseCommand and define a method named handle inside the class
from django.core.management.base import BaseCommand
class Command(BaseCommand):
""" Django command to pause execution until database is available"""
def handle(self, *args, **kwargs):
self.stdout.write("Custom command")
3) Now we can use the command
python manage.py CUSTOM_COMMAND