← Back to Django, page 4

django-0067 · Easy fresher

What are the most useful manage.py commands in Django, and how do dumpdata, loaddata, check and showmigrations work?

manage.py (or django-admin) runs management commands. Daily ones are runserver, makemigrations, migrate, createsuperuser, shell, test, collectstatic and check. dumpdata and loaddata export and import fixtures as JSON, showmigrations lists which migrations are applied, and check runs the system check framework to catch configuration problems early.

Full answer

Common commands:

  • runserver: development server with autoreload. Never use it in production.
  • makemigrations [app] and migrate [app] [name]: create and apply schema changes. sqlmigrate app 0003 prints the SQL; showmigrations lists applied ([X]) and unapplied ([ ]) migrations; makemigrations --check --dry-run fails in CI if model changes lack a migration.
  • createsuperuser, changepassword.
  • shell: a Python REPL with Django configured (shell -c "..." for one-liners).
  • check: runs system checks; check --deploy adds production checks.
  • test: runs the test suite; --parallel and --keepdb speed it up.
  • collectstatic: gathers static files into STATIC_ROOT for deployment.
  • dumpdata app.Model --indent 2 writes JSON fixtures; loaddata file.json loads them (objects with the same primary key are overwritten). dumpdata --natural-foreign avoids hard-coded foreign key IDs for content types. Fixtures suit small seed data and tests, not full backups; use database tools for backups.
  • dbshell: opens the database's own client. inspectdb generates models from an existing database. flush empties all tables. diffsettings shows settings that differ from defaults.
  • startapp, startproject: scaffolding.

python manage.py help <command> shows options. You can also write your own commands in management/commands/ and call any command from Python with call_command("check"), which is how tests and scripts reuse them.

python
import sys
import types

import django
from django.conf import settings

# Register a tiny in-memory "school" app so this example runs as one file
sys.modules["school"] = types.ModuleType("school")
sys.modules["school"].__file__ = __file__

settings.configure(
    SECRET_KEY="demo-only-key-not-for-production",
    ALLOWED_HOSTS=["testserver"],  # the host name used by Django's test client
    INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth", "school"],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
django.setup()

import io

from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core import checks
from django.core.management import call_command, get_commands
from django.db import connection

with connection.schema_editor() as editor:
    for model in (ContentType, Permission, Group):
        editor.create_model(model)

call_command("check")                       # raises SystemCheckError if a check fails
print("check problems:", checks.run_checks())

Group.objects.create(name="Teachers")
Group.objects.create(name="Students")
dump = io.StringIO()
call_command("dumpdata", "auth.group", stdout=dump)
print(dump.getvalue())

Group.objects.filter(name="Students").update(name="Changed locally")
with open("groups.json", "w") as f:
    f.write(dump.getvalue())
call_command("loaddata", "groups.json", verbosity=0)          # same primary keys are overwritten
print(sorted(g.name for g in Group.objects.all()))

known = get_commands()
print([c for c in ("runserver", "makemigrations", "migrate", "shell", "dumpdata", "loaddata", "check", "collectstatic") if c in known])

Standalone script. The first lines register a tiny in-memory app called school and an in-memory SQLite database so the file runs with `python file.py`. In a real project the settings live in settings.py and models live in an app's models.py. The snippet uses the built-in auth.group model because dumpdata skips apps without a models module, and it writes groups.json in the current folder. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): System check identified no issues (0 silenced). check problems: [] [{"model": "auth.group", "pk": 1, "fields": {"name": "Teachers", "permissions": []}}, {"model": "auth.group", "pk": 2, "fields": {"name": "Students", "permissions": []}}] ['Students', 'Teachers'] ['runserver', 'makemigrations', 'migrate', 'shell', 'dumpdata', 'loaddata', 'check']

Key points

  • makemigrations writes files; migrate applies them
  • check and check --deploy catch problems early
  • dumpdata and loaddata move small JSON fixtures
  • showmigrations and sqlmigrate show state and SQL
  • call_command runs commands from Python

Common mistakes

  • Using runserver in production
  • Treating dumpdata fixtures as database backups
  • Skipping makemigrations --check in CI
  • Loading fixtures that hard-code content type IDs

Follow-ups

  • How do you write a custom management command?
  • What does migrate --fake do and when is it risky?
  • How do you run a command from a test?

Tips

  • List commands by purpose
  • Mention the CI check
  • Name call_command