Framework cheatsheet
Django 6 Cheatsheet
Create projects, run migrations, test and deploy Django apps, plus the ORM queries you use every day.
Setup
- Create a virtual environment — Create an isolated environment for the project's packages.
- Install Django — Install Django into the active virtual environment.
- Check the Django version — Print the installed Django version.
- Create a project — Create a project in the current folder, so manage.py sits next to your repository root.
- Create an app — Create a new app inside the project. Add it to INSTALLED_APPS afterwards.
- Start the development server — Run the app at http://127.0.0.1:8000 with auto-reload.
- Change the address and port — Listen on all interfaces on port 8080.
Database and migrations
- Create migrations — Generate migration files from changes to your models.
- Apply migrations — Apply pending migrations to the database.
- List migrations — Show every migration and whether it has been applied.
- See a migration's SQL — Print the SQL a migration would run, without running it.
- Roll back to an earlier migration — Unapply migrations after the named one. Use zero to unapply all of an app's migrations.
- Open the database shell — Open psql, mysql or sqlite3 using the settings in DATABASES.
- Generate models from an existing database — Print model classes for tables in an existing database.
- Export data as JSON — Write an app's data to a fixture file.
- Import a fixture — Load data from a fixture file into the database.
- Delete all data — Remove all data from the database and re-run post-migrate setup.
Users and shell
- Create an admin user — Create a superuser who can sign in to /admin/.
- Change a user's password — Set a new password for a user, prompting for it.
- Open the Django shell — Start a Python shell with your project loaded.
Testing and checks
- Run the tests — Run every test in the project.
- Run one test — Run a single test method by its dotted path.
- Run tests in parallel — Use one process per CPU core.
- Reuse the test database — Keep the test database between runs to save time.
- Check the project for problems — Run Django's system checks.
- Check deployment settings — Run the extra security and production checks.
Static files and deployment
- Collect static files — Copy static files into STATIC_ROOT for the web server to serve.
- Generate a secret key — Print a new random value for SECRET_KEY.
- See changed settings — Show settings that differ from Django's defaults.
- Run with Gunicorn — Serve the project with a production WSGI server.
ORM queries
- Create a row — Insert a new object into the database.
- Get one object — Fetch a single object. It raises DoesNotExist or MultipleObjectsReturned otherwise.
- Filter and sort — Get published posts, newest first.
- Search with a lookup — Find posts whose title contains a word, ignoring case.
- Update many rows — Update all matching rows with one query.
- Delete rows — Delete all matching rows.
- Avoid N+1 queries — Fetch a foreign key in the same query. Use prefetch_related for many-to-many and reverse relations.
- Count related objects — Add a count of comments to each post.
Related cheatsheets
- Angular 22 cheatsheet — Angular CLI commands and standalone component, signals, template, routing, HTTP and forms snippets.
- Astro cheatsheet — Build fast content sites with Astro: components, layouts, routing, content collections, islands, endpoints and deployment.
- .NET and ASP.NET Core 10 cheatsheet — The dotnet CLI for creating, building, testing and publishing projects, plus common ASP.NET Core and EF Core code.
- Express 5 cheatsheet — Build HTTP servers and APIs with Express 5 on Node.js.
- FastAPI 0.115 cheatsheet — Install, run, test and deploy FastAPI apps from the command line.
- Flask 3 cheatsheet — Create, run, test and deploy Flask apps, with routes, templates, blueprints, sessions and database snippets.