Django Shell Basics
In this page:
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.
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.
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.
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.
exit()
⚠️ Run this command in your terminal.
- Trying to import a model without knowing its app, forgetting the shell still needs the correct
from app.models import Modelline. - 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.
- Confusing the Django shell with the plain
pythoninterpreter, which has no access to your models until Django is explicitly set up.
- 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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: