Skip to main content

Command Palette

Search for a command to run...

Making your life easier with 'run script' in Django

This post assumes you have prior knowledge of basic python and Django.

Published
3 min read
Making your life easier with 'run script' in Django
S

I'm a full-stack web developer with Django(Python) and React js as my main stack. I'm also a beginner writer, who loves to write tutorials for different purposes.

A basic python script is some code in python that you directly run through the command line and it gives you an output. For example, if you want to print "hello world"

print("hello world")

Screenshot from 2022-06-03 09-04-11.png

You can also save the script in a file and run it through the command line to execute that code.

$ python3 hello.py

Screenshot from 2022-06-03 09-08-18.png

These are just a few demos of what you can do in python.

Scripts in Django

After you set up a project in Django, It's quite a hassle to manually check your models and objects through the Django shell command. Let's create a simple Django app (considering you have already installed and set up Django).

class Students(models.Model):
    first_name = models.CharField(max_length=200, null=True, blank=True)
    last_name = models.CharField(max_length=200, null=True, blank=True)
    address = models.CharField(max_length=200, null=True, blank=True)
    dob = models.DateField(auto_now_add=False, null=True, blank=True)

    def __str__(self):
        return self.first_name + self.last_name

After creating the model run makemigrations and migrate command in the command line.

python manage.py makemigrations && migrate

Screenshot from 2022-06-03 09-21-55.png

Now we can add some students through the Django shell.

python manage.py shell

An interactive console will open in your terminal.

Screenshot from 2022-06-03 09-25-17.png

Now we can create our first student.

from students.models import Students
Students.objects.create(first_name='John', last_name='Doe', address='12th street, Washington')

Screenshot from 2022-06-03 09-31-01.png

This process can be tedious if we have to repeat it for many students and will consume a lot of time. But wait there's an easy trick to do this. This is where Django's runscript command comes in.

In your command line write the following line:

pip install django-extensions

Screenshot from 2022-06-03 09-38-33.png

Add it to the INSTALLED_APPS on your settings file:

INSTALLED_APPS = [
    ...,
    'django_extensions',
]

Now we just have to create a folder called scripts inside our main project folder (where our manage.py exists).

mkdir scripts
touch scripts/__init__.py

Screenshot from 2022-06-03 09-44-51.png

Now we can write our scripts to add, remove or modify our students. Let's create a script to delete all students in our database.

touch scripts/delete_all_students.py
from students.models import Students

def run():
    #fetch all students
    students = Students.objects.all()
    #delete all students
    students.delete()

Now that our script is ready, we can just run our script through the command line as follows:

python manage.py runscript delete_all_students

If you now check the student's model you'll find that all students have been removed.

python manage.py shell
from students.models import Students
Students.objects.all()

Screenshot from 2022-06-03 10-06-47.png

We have an empty Queryset here, meaning all the students have been removed. This is just the tip of the iceberg and you can do almost every shell command from your runscript command. The only limitation is your imagination.

TLDR;

You can use python manage.py runscript <script> to run various time-consuming shell commands directly in an instant. If you want to know more about the runscript command visit here.