1. Introduction
This is a comparison between dbshell and the Django shell. What I am going to do is to do the same insert task with these different shells. As shown below, they can start by:
python manage.py dbshell
and
python manage.py shell
The entrances are similar, but the ways to do things are quite different.
First, I will show you the general difference. Then, I will show you more specifics.
2. The difference
dbshell, django shell, difference
Great question! Here's the difference between dbshell and shell in Django:
🐚 python manage.py dbshell
-
Purpose: Opens a command-line interface to interact directly with your database using SQL.
-
Use case: Run raw SQL commands like
SELECT,INSERT, orDELETE.
Example:
Inside dbshell, you can write:
🐍 python manage.py shell
-
Purpose: Opens a Python shell with Django loaded.
-
Use case: Use Django models and ORM to interact with your database via Python objects.
Example:
Inside shell, you can write:
⚖️ Summary:
| Command | Access Level | Language Used | Use When You Want To... |
|---|---|---|---|
manage.py dbshell
|
Raw DB Access | SQL | Run direct SQL queries on the DB |
manage.py shell
|
Django ORM & Python | Python | Work with models and test Django code |
Let me know which one you want to try, or if you need help with any specific command!
3. dbshell
(1) Convesation with GPT
On dbshell, how can I add a data to a table? I want to add a data to polls_question.
To add data to the polls_question table using dbshell, you'll
use a standard SQL INSERT command. Here's a step-by-step guide:
✅ 1. Open the Django dbshell:
✅ 2. Use an INSERT INTO SQL command
Assuming your polls_question table has at least the following fields:
-
id(usually auto-incremented) -
question_text -
pub_date(a datetime)
You can run something like:
📝 Note:
-
The date/time format must be
YYYY-MM-DD HH:MM:SS -
Make sure the
polls_questiontable name is correct (Django adds the app name as a prefix by default)
✅ 3. Check the result
Run:
To confirm the new row was inserted.
🛑 Tip:
If you're unsure about the column names, run:
This will show you the exact structure of the table.
Let me know if you want help creating a pub_date for “now” or another specific datetime!
(2) What I did
I entered dbshell, and checked tables.
I selected a table, and inserted a data into it:
I did the same, then exited.
4. Django shell
(1) Conversation with GPT
This time, I want to do the same things on Django shell. How can I add a data to a table? I want to add a data to polls_question.
To add data to the polls_question table using the Django shell, you’ll use Django’s ORM
(Object-Relational Mapping), not SQL.
✅ 1. Open the Django shell:
✅ 2. Import the Question model:
✅ 3. Create and save a new question:
Now you've added a row to the polls_question table.
✅ 4. Confirm it's saved:
Or view specific fields:
Let me know if you'd like to add choices for the question too!
(2) What I did
I entered shell, and added a data:
I tried to see the table, but it was not easy to read, so I used another way to see the table. Finally, I exited it.
5. My Polls App
My polls app showed the change of the table immediately after the change. It shows the latest 5 questions except future ones, as of 2025-07-08 09:00:00 GMT.
