How to execute code in a Django shell using an external python script?

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?

+7
python linux django shell manage.py
source share
5 answers

First, you should not access your Python shell using sudo . There is no need to run as root.

Secondly, the way to create a script that is run from the command line is to create a custom manage.py script file so you can run ./manage.py deactivate_users . Full instructions for this are in the documentation .

+8
source share

Try entering commands into the current django shell as a document here:

 $ sudo python manage.py shell << EOF user = User.objects.get(username=FooBar) user.is_active = False user.save() exit() EOF 
+3
source share

If you want to execute a Python script that accesses Django models, you first need to set the environment variable:

 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<path>.settings") 

In which you need to replace <path> with the project directory, which contains the settings.py file.

Then you can import the model files, for example:

 from <path>.models import User user = User.objects.get(username=FooBar) user.is_active = False user.save() 
+2
source share

Based on Daniel's comment earlier in this thread, I created a simple script to do this work. I share this for readers of this topic who are trying to achieve the same goal. This script will create the manage.py deactivate_user working function.

This is an example of a link to your Django application folder structure:

Django folder structure

You want to create the deactivate_user.py file and place it in the management / commands / deactivate_user.py directory.

 from django.core.management.base import BaseCommand, CommandError from django.contrib.auth.models import User class Command(BaseCommand): help = 'Deactivate user in the database' def handle(self, *args, **options): username = raw_input('Please type the username of the user you want remove: ') try: user = User.objects.get(username=username) user.is_active = False user.save() print ('User is now deactivated in the database.') except User.DoesNotExist: print ('Username not found') 

Call the script using python manage.py deactivate_user . You can also create an "activate_user" script with the same code, but instead of user.is_active = False use = True.

0
source share

Open Django python manage.py shell

Then run execfile('filename.py')

0
source share

All Articles