Skip to content

Django Cheat Sheet

programming-banner

Introduction

The article covers several important database operation commands such as createcachetable, dbshell, dumpdata, flush, inspectdb, loaddata, makemigrations, migrate, showmigrations, sqlflush, sqlmigrate, sqlsequencereset, squashmigrations, and testserver. These commands will help you to manage your database more efficiently and will save you a lot of time.

Moreover, the article also covers several environment configuration commands such as startproject, startapp, compilemessages, and makemessages. These commands will help you to set up your Django environment quickly and easily.

Lastly, the article covers development debugging commands such as check, diffsettings, runserver, shell, sendtestemail, and test. These commands will help you to debug your application and test it thoroughly.

Database operation commands

createcachetable

Create a data table for caching, which is used to support the caching middleware declared in the configuration file.

django-admin createcachetable

--database DATABASE
## Used to specify the database where the cache table is generated. By default, this parameter specifies the database set in the default setting of the configuration file.

dbshell

Use the database engine set in the configuration file through the command line.

django-admin dbshell

--database

dumpdata

Export the database table data of the specified application.

django-admin dumpdata [app_label[.ModelName]

--all
--format
--database
--output
--indent
--exclude

flush

Remove the data from the business table in the database, while the tables related to the records generated by migrate are not deleted.

django-admin flush

--database

inspectdb

Check all tables in the database and generate output model classes for each table.

django-admin inspectdb [table [table ...]]

--database

loaddata

Query fixed information content and import it into the database.

django-admin loaddata fixture [fixture ...]

--database
--ignorenonexistent
--app
--exclude

makemigrations

Generate a database migration script file based on the detection result of the model in the project.

django-admin makemigrations [app_label [app_label ...]]

--empty
--dry-run
--merge
--name
--check

migrate

Synchronize the database based on the model and migration file information.

django-admin migrate [app_label] [migration_name]

--database
--plan
--check

showmigrations

View the existing database synchronization scripts in the project.

django-admin showmigrations [app_label [app_label ...]]

--list
--plan
--database

sqlflush

View the script that clears the database.

django-admin sqlflush

--database

sqlmigrate

View the SQL statement of the specified name database synchronization.

django-admin sqlmigrate app_label migration_name

--backwards
--database

sqlsequencereset

Generate a script corresponding to the sequence used in the table related to the specified application.

django-admin sqlsequencereset app_label [app_label ...]

--database

squashmigrations

Generate merged migration files and merge multiple migration script files under the application into a few possible ones. The migration files before merging and after merging can coexist.

django-admin squashmigrations app_label [start_migration_name]migration_name

--no-optimize
--noinput, --no-input
--squashed-name

Environment configuration command

startproject

Create a Django project directory framework with the specified project name in the current directory or given address.

django-admin startproject name [directory]

startapp

Create an app.

django-admin startapp name [directory]

--template
--extension
--name

compilemessages

Compile .po language files (generated by the makemessages method) for international language support.

django-admin compilemessages

--locale
--exclude

makemessages

Check and extract the information to be translated in all files in the project file.

django-admin makemessages

--all
--extension
--locale
--exclude
--ignore

Development debugging commands

check

Check if there are potential problems in the overall Django project and whether the required associated files of the project are complete. By default, check all applications under the project.

django-admin check [app_label [app_label ...]]

--tag
--list
--deploy
--database

diffsettings

Display the differences between the current project’s configuration file and the default Django configuration file.

django-admin diffsettings

--all
--output

runserver

Start locally.

django-admin runserver [addr:port]

--noreload
--nothreading
--ipv6

shell

Enter the Django shell.

django-admin shell

--nostartup
--command

sendtestemail

Send a test email.

django-admin sendtestemail [email [email ...]]

--managers
--admins

test

Test the Django project.

django-admin test [test_label [test_label ...]]

--failfast
--testrunner
--noinput
--keepdb
--reverse
--debug-mode
--debug-sql
--parallel
--buffer

testserver

Use the given data to run the Django development server.

django-admin testserver [fixture [fixture ...]]

--addrport
Leave a message