Django 6 cheatsheet

Django 6: Database and migrations

10 common Django 6 tasks.

Create migrations

Generate migration files from changes to your models.

python manage.py makemigrations

Apply migrations

Apply pending migrations to the database.

python manage.py migrate

List migrations

Show every migration and whether it has been applied.

python manage.py showmigrations

See a migration's SQL

Print the SQL a migration would run, without running it.

python manage.py sqlmigrate blog 0001

Roll back to an earlier migration

Unapply migrations after the named one. Use zero to unapply all of an app's migrations.

python manage.py migrate blog 0001

Caution: Rolling back can drop columns or tables and their data.

Open the database shell

Open psql, mysql or sqlite3 using the settings in DATABASES.

python manage.py dbshell

Generate models from an existing database

Print model classes for tables in an existing database.

python manage.py inspectdb > models.py

Export data as JSON

Write an app's data to a fixture file.

python manage.py dumpdata blog --indent 2 > blog.json

Import a fixture

Load data from a fixture file into the database.

python manage.py loaddata blog.json

Delete all data

Remove all data from the database and re-run post-migrate setup.

python manage.py flush

Caution: Deletes every row in every table. Use it only on a development database.

Was this page helpful?