How to script django shell operations? - python

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.

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()

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.

Related

Django - pass commands to shell

I have an django project on Heroku and I need to update the DB daily. Manually i would open manage.py shell and write there this:
from app import views
views.function()
One way i found to do that automatic is through a heroku scheduler, however I would like to know if it is possible to tell the shell what commands should it run.
I was doing this:
python -c "from app import views;views.function"
but it gives me an error because that should be done on the shell instead of the command line, so is it possible to tell the shell what should it write?
Thanks :D
You can write a custom django command, something like my_command.py and call it from the command line:
python manage.py my_command
https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/

which command do i use to execute this flask( python ) file?

I'm learning flask through tutorials.Now I stuck on making database file because they just provide unix command to execute that script my question is which command do i use if i want to install that "db_create.py" file from command prompt that i've mention below..I'm running on virtual enviroment with directory project in cmd.
#!flask/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path
db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO,api.version(SQLALCHEMY_MIGRATE_REPO))
I have also install SQLAlchemy.
If it's already specified in the first line the interpreter of this script, then you just have to grant the script the executable permits in order for it to run.
Hence just type the following command, if you are on UNIX-based machine:
sudo chmod +x <path_to_python_script>
And then just execute:
./<path_to_python_script>
Otherwise if you are on a Windows-based machine, move into the folder of the script and just run:
python -m db_create.py
Hope it works!

How do I pipe a model query into the Django Shell via a Bash Script?

I'm writing a startup.sh script to be ran when a docker container is created.
#!/bin/bash
python manage.py runserver
python manage.py makemigrations accounts
python manage.py migrate
python manage.py check_permissions
python manage.py cities --import=country --force
*python manage.py shell | from cities.models import * Country.objects.all().exclude(name='United States").delete()*
python manage.py cities --import=cities
python manage.py cities --import=postal_code
I am guessing the line in question is incorrect, what would be the correct way to do this in a bash script?
Use a heredoc:
python manage.py shell <<'EOF'
from cities.models import *
Country.objects.all().exclude(name='United States').delete()
EOF
It's not such a good idea to include django code in a shell script file. It's better to either make a python file and put those code in it and do:
python manage.py shell < script.py
Or better, write a django management command. In this way you could track your code in the same project/repo and people got less confused when they see this.

How to automate django dumpdata?

I am populating my DB locally and I want to dump that data to the production server with a script for all my apps.
I am trying to write a script that will do this...
$ source path/to/venv && python manage.py dumpdata app1 > file1.json
$ source path/to/venv && python manage.py dumpdata app2 > file2.json
...etc
I use fabric for my deploy script and I thought it would be nice to incorporate it in there, but the 'local' method in fabric doesn't seem to be able to do such a thing. the run command does, but IDK why.
I think it might have something to do with this...
local is not currently capable of simultaneously printing and
capturing output, as run/sudo do. The capture kwarg allows you to
switch between printing and capturing as necessary, and defaults to
False. (http://docs.fabfile.org/en/latest/api/core/operations.html)
but I am not sure
I tried doing it with os.system n a separate python script as well but that didn't work either, both of them give me the same error which is...
sh: 1: source: not found
I have checked and double checked the path many times, I can't seem to figure it out. What do you think?
Your script executes under the classic sh shell, not under bash. "source" is a bash command; the classic import command is a period (like ". pathto/pyenv/bin/activate"). Or you could force bash with #!/bin/bash at the start of your script.
Since '$ source' was the thing that could not be executed. I made a shell script, placed it in a directory and just executed that
source pathto/pyenv/bin/activate && python manage.py dumpdata quiz > data_dump/foo.json
source pathto/pyenv/bin/activate && python manage.py dumpdata main > data_dump/bar.json
source pathto/pyenv/bin/activate && python manage.py dumpdata study > data_dump/waz.json
and then in the fabric file...
def foobar():
local('/pathto/foo.sh')

Django: manage.py standard commands not working

I'm developing a web app in Python 2.7 using Django 1.4 with PyCharm 2.5 as my IDE and a Postgres database. I am able to run manage.py commands such as sql and syncdb to create the SQL and the tables, but other commands are not recognized. When I attempt to run sqlreset (or any other command that drops tables or alters data), I get an "Unknown command" error:
runnerw.exe C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 2.5.1\helpers\pycharm\django_manage.py" sqlreset EventMapperApp C:/Users/Karen/PycharmProjects/eventsMap
Unknown command: 'sqlreset'
Type 'manage.py help' for usage.
Process finished with exit code 1
Could anyone help me figure out what's going on?
Are you sure you are running Django 1.4? sqlreset has been deprecated since 1.3, I think, and is slated to be removed in 1.5. It is present in Django 1.4, but has been removed in the development version.
There isn't a sqlreset.
Info here: https://docs.djangoproject.com/en/dev/ref/django-admin/
or manage.py help as your post shows...
Django's management commands don't automatically run commands that may destroy your database (commands such as altering a table, or changing the type of columns).
If you want to completely reset your application - as if you just ran syncdb, you need to do it manually.
I wrote this gist that will reset your database. It is not portable (only works on *inx-like systems), but it might help you out.
Note: this will delete (drop) everything.
echo 'from django.conf import settings; print settings.INSTALLED_APPS; quit();' | \
python manage.py shell --plain 2>&1 | \
tail -n1 | sed -r "s|^.*\((.*)\).*$|\1|; s|[',]| |g; s|django\.contrib\.||g" | \
xargs python manage.py sqlclear | \
python manage.py dbshell && python manage.py syncdb

Categories