📝 Django

Django Shell

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
81
🌱
Level
Beginner

Django Shell is an interactive Python console with the full Django environment loaded.

Starting the Shell

python manage.py shell

Django automatically loads settings, models, and all dependencies.

Core Operations

# Import models
from tasks.models import Task, Project
from django.contrib.auth.models import User

# Creating objects
user = User.objects.create_user('testuser', password='pass123')
project = Project.objects.create(name='My Project')
task = Task.objects.create(title='First Task', owner=user, project=project)

# Reading
Task.objects.all()
Task.objects.filter(status='todo')
task = Task.objects.get(pk=1)

# Updating
task.status = 'done'
task.save()

# Deleting
task.delete()

Debugging Queries

from django.db import connection

Task.objects.all()
print(connection.queries)  # list of executed SQL queries

Testing Code

# Check business logic
task = Task.objects.first()
print(task.get_status_display())
print(str(task))

# Inspect a queryset
qs = Task.objects.select_related('project').filter(owner__username='testuser')
print(qs.query)  # show the SQL

IPython Shell (Enhanced)

pip install ipython
python manage.py shell  # automatically uses IPython

Features: tab completion, command history, syntax highlighting, ? for help.

shell_plus (django-extensions)

pip install django-extensions ipython
# settings.py
INSTALLED_APPS = [
    ...
    'django_extensions',
]
python manage.py shell_plus
# Automatically imports ALL models!

On startup, all models are imported automatically:

from tasks.models import Task, Project
from django.contrib.auth.models import User
# and every other model from every app
python manage.py shell_plus --ipython

IPython features: Tab completion, ? and ?? for docs, syntax highlighting, command history.

In [1]: Task?           # class help
In [2]: Task.objects.   # Tab to autocomplete methods
In [3]: %sql Task.objects.all()   # debug SQL
In [4]: %run script.py            # load a script

Jupyter Notebook

python manage.py shell_plus --notebook

Opens a Jupyter Notebook with the full Django environment — great for data analysis.

Other useful django-extensions commands

python manage.py graph_models -a -o models.png   # diagram of all models
python manage.py show_urls                         # list all URLs
python manage.py runserver_plus                    # werkzeug debugger
python manage.py create_command myapp              # scaffold a management command

Running a Script

python manage.py shell < script.py
# or
python manage.py shell -c "from tasks.models import Task; print(Task.objects.count())"

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 132
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 85
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 73

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!