1

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.

2 Answers 2

3

I guess the best option would be to write a custom management command.

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this elegant solution. For simple stuff I will use the @wolendranh solution but remind I can handle it this way.
1

Or you can try this one line solution:

echo "from myapp.scenarios import *; reset_demo_data(); exit()" | python manage.py shell

You can add this line replacing Shell activation line.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.