# Making your life easier with 'run script' in Django

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654226392242/Xuu2lfxAL.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654226610100/nfCYDjvzV.png align="left")

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

```python
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654227429997/Lo8mBr8R6.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654227634172/LXVX2221S.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654227971789/TCfpWvCeP.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654228438331/EE6om66yM.png align="left")

Add it to the INSTALLED_APPS on your settings file:
```python
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654228803544/-BcdCke18.png align="left")

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
```

```python
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1654230121848/wN69oA8Ej.png align="left")


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](https://django-extensions.readthedocs.io/en/latest/runscript.html).








