Django: 3 steps adding shell_plus to make debugging easily in importing models.

Grassroot Engineer
2 min readOct 6, 2024

--

What is shell_plus?

Django’s built-in shell is great, but it requires you to manually import every model and function you need each time you launch it. This is where shell_plus comes in handy. It automatically imports all your models, making it easier to jump right into debugging without writing repetitive import statements.

https://www.youtube.com/watch?v=kNFMpfek4Ic

Let’s walk through how to install it and how to use it effectively.

  1. Install Django Extensions

Open your terminal and run the following command to install Django Extensions using pip:

pip install django-extensions

Or if you are using docker and requirements.in you can put here.

# Debugging only on Local environment
django-extensions

The run this to get real package in requirements.txt

docker-compose exec django sh -c "pip install pip-tools && pip-compile"

2. Add Django Extensions to Installed Apps

After installing the package, you need to add it to your Django project’s INSTALLED_APPS so Django knows to include it. Open your settings.py file and add 'django_extensions' to the INSTALLED_APPS section like this:

# settings.py

INSTALLED_APPS = [
# Your other apps...
'django_extensions',
]

3. Now you can running shell_plus by

python mange.py shell_plus

But it’ll be better if you are using docker and want to run short bash, so can make Makefile and put this.

shell:
docker-compose exec django python manage.py shell_plus
Example to use shell_plus by Makefile

Conclusion

If you frequently work in Django’s shell for debugging or querying data, shell_plus can make it quickly. It reduces the time spent on repetitive tasks like importing models and allows you to focus on solving the actual problem at hand.

Refs:
https://django-extensions.readthedocs.io/en/latest/shell_plus.html

If you think it’s useful for you, just clap your hands 👏 to be encouraged me.

GRASSROOT ENGINEER 😘

--

--

Grassroot Engineer
Grassroot Engineer

Written by Grassroot Engineer

ATM engineer who is interested in CODING and believe in EFFORT. — https://grassrootengineer.com

No responses yet