· 

Django: dbshell vs shell

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, or DELETE.

Example:

bash
python manage.py dbshell

Inside dbshell, you can write:

sql
SELECT * FROM polls_question;

🐍 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:

bash
python manage.py shell

Inside shell, you can write:

python
from polls.models import Question
Question.objects.all()

⚖️ 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:

bash
python manage.py 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:

sql
INSERT INTO polls_question (question_text, pub_date)
VALUES ('What is your favorite color?', '2024-06-17 12:00:00');

📝 Note:

  • The date/time format must be YYYY-MM-DD HH:MM:SS

  • Make sure the polls_question table name is correct (Django adds the app name as a prefix by default)


✅ 3. Check the result

Run:

sql
SELECT * FROM polls_question;

To confirm the new row was inserted.


🛑 Tip:

If you're unsure about the column names, run:

sql
.schema polls_question

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:

bash
python manage.py shell

✅ 2. Import the Question model:

python
from polls.models import Question
from django.utils import timezone

✅ 3. Create and save a new question:

python
q = Question(question_text="What is your favorite color?", pub_date=timezone.now())
q.save()

Now you've added a row to the polls_question table.


✅ 4. Confirm it's saved:

python
Question.objects.all()

Or view specific fields:

python
for q in Question.objects.all():
print(q.id, q.question_text, q.pub_date)

 

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.