How to script django shell operations?
Question: I'd like to create a script (.sh or python, not important) that can do the
following:

    
    
    heroku pg:reset DATABASE_URL
    heroku run python manage.py migrate
    heroku run python manage.py shell
    > from myapp.scenarios import *; reset_demo_data(); exit()
    

Line 1 to 3 are UNIX commands, but Line 4 is python to be executed in the
opened Django shell.

I tried stuff with `|` and `>` to "inject" the python code in the command but
nothing worked. I guess it's quite easy to do but I can't figure out how..

Thanks.


Answer: I guess the best option would be to write a [custom management
command](https://docs.djangoproject.com/en/1.7/howto/custom-management-
commands/).

Your script could then look like:

    
    
    heroku pg:reset DATABASE_URL
    heroku run python manage.py migrate
    heroku run python manage.py shell
    heroku run python manage.py reset_demo
    

when the management command is something like:

    
    
    from django.core.management.base import BaseCommand
    from myapp.scenarios import *
    
    class Command(BaseCommand):
        def handle(self, *args, **options):
            reset_demo_data()
    

