How to remove, drop a table in a database with the django web framework ?

Published: March 21, 2022

Updated: December 09, 2022

Tags: Python; Django;

DMCA.com Protection Status

Example of how to drop a table in a database with django:

Start django database shell

In the terminal go the the folder with the manage.py file and then enter the following command:

python manage.py dbshell

It is going to start a shell associated with the database type. For example with the default SQLite it will returns:

SQLite version 3.35.4 2021-04-02 15:20:15
Enter ".help" for usage hints.
sqlite>

SQLite database

Get all tables name

To get all table names just enter:

sqlite> .tables

it will returns for example:

sqlite> .tables
        auth_group                        
        auth_group_permissions               
        auth_permission                     
        auth_user                                    
        auth_user_groups             
        myapp_article

Get the number of rows in a table

To get the number of rows in a table

sqlite> SELECT COUNT(*) FROM myapp_article;
1563

Drop a given table

To drop a table:

DROP TABLE myapp_article;