← Back to Django Course | Chapter 5: Migrations & Database Management | Lesson 6 of 8

Django Shell Basics

The Django shell is a regular Python prompt that already knows about your models, so you can query and change your data by typing commands instead of writing a script.

Opening the Shell

Running manage.py shell starts an interactive Python session with Django's settings already loaded, so models and the database are ready to use immediately.

Example: Opening the Shell

This drops you into a Python prompt (>>>) where Django is fully configured — no manual setup needed before importing models.

bash
python manage.py shell

⚠️ Run this command in your terminal.

Importing and Querying a Model

Inside the shell you import a model the same way you would in a views.py file, then call methods like objects.all() directly on it.

Example: Importing and Querying a Model

This prints a QuerySet showing every Article row currently stored in the database.

bash
from blog.models import Article
Article.objects.all()

⚠️ Run this command in your terminal.

Creating and Saving Objects

You can build a new model instance and call .save() on it right from the shell, which writes it to the database immediately, just like a view would.

Example: Creating and Saving Objects

After save() runs, a new row exists in the article table — this change is permanent, not undone by closing the shell.

bash
a = Article(title='My First Post')
a.save()

⚠️ Run this command in your terminal.

Exiting the Shell

Typing exit() or pressing Ctrl+D closes the shell and returns you to your regular terminal prompt; nothing about your saved data is lost.

Example: Exiting the Shell

This ends the interactive session; any objects already saved with .save() remain in the database afterward.

bash
exit()

⚠️ Run this command in your terminal.

Common Mistakes
  1. Trying to import a model without knowing its app, forgetting the shell still needs the correct from app.models import Model line.
  2. Making changes in the shell and expecting them to disappear when you close it, when saved objects persist in the real database just like any other write.
  3. Confusing the Django shell with the plain python interpreter, which has no access to your models until Django is explicitly set up.
Chapter Summary
  • python manage.py shell opens a Python prompt with Django already configured.
  • You can import and query any model exactly as you would in a view.
  • Changes you save in the shell are real, permanent writes to the database.
  • It's the fastest way to experiment with the ORM without writing a view.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.