What I want to achieve:
I would like to create a python script to deactivate Django users in a database from the CLI. I came up with this:
$ sudo python manage.py shell >>> user = User.objects.get(username=FooBar) >>> user.is_active = False >>> user.save() >>> exit()
The above code WORKS when I manually enter it manually after the command. However, I would like to execute the execution of commands in one .py script as
$ sudo python script.py
Now I have tried different arguments:
os.system("command1 && command2 && command3")subprocess.Popen("command1 && command2 && command3", stdout=subprocess.PIPE, shell=True)
Problem:
This does not work! I think this problem is here because Python waits until the open Django shell (first command) opens, which never will be. It does not execute the rest of the commands in the script since the first command puts it on Hold.
subprocess.popen can execute commands in the shell, but only in the Python shell, I would like to use the Django shell.
Does anyone know how to access the Django shell using a .py script to execute custom code?
python linux django shell manage.py
scre_www
source share