Django CLI Cheat Sheet

Django

The commands I use to create and manage a Django project: django-admin for anything outside a project, manage.py for everything inside one.

Project Initialization (django-admin)

django-admin is installed with Django and works from anywhere.

# List every available subcommand
django-admin --help

# Show the help page of one subcommand
django-admin help <command_name>

# Start a new project
django-admin startproject <project_name>

Management (manage.py)

startproject creates manage.py at the project root. It sets the project's settings module, so run these commands from the folder that contains manage.py.

Server

# Start the local development server
python manage.py runserver

Users

# Create an admin account (prompts for username, email and password)
python manage.py createsuperuser

Apps in a Subfolder

# From the folder containing manage.py
mkdir -p apps/blog
python manage.py startapp blog apps/blog

Then set name = "apps.blog" in apps/blog/apps.py and add "apps.blog" to INSTALLED_APPS; the app label stays blog.

Migrations

Each migration is a Python file describing one step that brings the database schema in line with the models.

# Generate migration files from model changes
python manage.py makemigrations

# Apply pending migrations to the database
python manage.py migrate

# List all migrations and whether they are applied
python manage.py showmigrations

# Print the raw SQL a migration will execute
python manage.py sqlmigrate <app_name> <migration_number>

# Revert all of an app's migrations
python manage.py migrate <app_name> zero

# Revert to a specific migration (prefix of its file name, e.g. 0021)
python manage.py migrate <app_name> 0021

# Create an empty migration file (for data migrations)
python manage.py makemigrations <app_name> --empty

# Squash a range of migrations into one
python manage.py squashmigrations <app_name> <start_migration> <end_migration>

A data migration fills the empty file with a RunPython operation. Use apps.get_model() rather than importing the model, so the migration sees the model as it was at that point in history:

from django.db import migrations

def archive_to_completed(apps, schema_editor):
    Task = apps.get_model("tasks", "Task")
    Task.objects.filter(status="ARCHIVED").update(status="COMPLETED")

class Migration(migrations.Migration):
    dependencies = [("tasks", "0001_initial")]
    operations = [migrations.RunPython(archive_to_completed)]

Fixtures

# Dump an app's data as seed data
python manage.py dumpdata accounts --indent 2 --natural-foreign > apps/accounts/fixtures/data.json

# Load it into another database
python manage.py loaddata data.json

Database Shells

# Open the SQL client of the configured database
python manage.py dbshell

# Open a Python REPL with Django loaded (for ORM testing)
python manage.py shell

# Same, with every model auto-imported (needs django-extensions)
python manage.py shell_plus

References

Review cards

Flashcard What does `python manage.py shell_plus` provide?

It opens a Python REPL with every model auto-imported, useful for ORM testing.

Quiz Which command would you use to start a new app named 'blog' inside a subfolder 'apps/blog'?
  • python manage.py startapp blog apps/blog
  • django-admin startapp blog apps/blog
  • python manage.py startapp blog
  • django-admin startproject blog apps/blog

The startapp command must be run from the folder containing manage.py.

Flashcard How can you revert a specific migration in an app to a previous state?

Use python manage.py migrate <app_name> 0021 to revert to a specific migration.

Flashcard What is RunPython for in a data migration, and what makes it reversible?

It runs Python code that changes data, not schema; use apps.get_model() to get the historical model. Pass a reverse function (or RunPython.noop) so that migrate can go back past it.