Django Commands Cheat Sheet

Django Commands Cheat Sheet

Django, a high-level Python web framework, provides a set of management commands that help you perform various tasks during the development and management of a Django project. Here’s a comprehensive list of Django management commands along with brief descriptions of their features:

startproject

Creates a new Django project.

django-admin startproject projectname
startapp

Creates a new Django app within a project.

python manage.py startapp appname
runserver

Starts the development server.

python manage.py runserver
shell

Opens the Python shell with Django environment loaded.

python manage.py shell
makemigrations

Generates new database migration files based on model changes.

python manage.py makemigrations
migrate

Applies database migrations to synchronize the database schema.

python manage.py migrate
createsuperuser

Creates a superuser for the Django admin.

python manage.py createsuperuser
collectstatic

Gathers static files from your apps into a single directory.

python manage.py collectstatic
test

Runs tests for your Django project.

python manage.py test
dbshell

Opens a command-line interface to the database.

python manage.py dbshell
check

Checks for issues in your project without making migrations or touching the database.

python manage.py check
showmigrations

Displays a list of all migrations and their status.

python manage.py showmigrations
shell_plus

Enhanced version of the shell with additional features (requires django-extensions).

python manage.py shell_plus
dumpdata

Outputs the contents of the database as a JSON or XML fixture.

python manage.py dumpdata
loaddata

Loads data from a fixture into the database.

python manage.py loaddata
flush

Resets the database by removing all data.

python manage.py flush
createsuperuser

Creates a superuser for the Django admin.

python manage.py createsuperuser
startapp

Creates a new app within a Django project.

python manage.py startapp appname
runserver

Starts the development server.

python manage.py runserver
runscript

Runs a Python script in the context of a Django project (requires django-extensions).

python manage.py runscript script_name
graph_models

Creates a visual representation of your Django models (requires django-extensions).

python manage.py graph_models -a > models.dot
inspectdb

inspectdb looks at your database tables and prints out Django models. The biggest use case for this is if you have a legacy database that you want to use with Django. The script looks at the database and creates Django models for each table it find.

python manage.py inspectdb

# output of this command will look something like this:
from django.db import models

    class MyTable(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=255)
        created_at = models.DateTimeField()
    
        class Meta:
            managed = False
            db_table = 'my_table'

Pro tip: Pass a table name to build models for just that table.

Read more about inspectdb in the Django docs .

dbshell

Opens a command-line interface to the database.

python manage.py dbshell
shell_plus

Enhanced version of the shell with additional features (requires django-extensions).

python manage.py shell_plus
test

Runs tests for your Django project.

python manage.py test
check

Checks for issues in your project without making migrations or touching the database.

python manage.py check
check —deploy

Checks for common issues in a deployment-ready project.

python manage.py check --deploy
show_urls

Displays all URLs defined in the project.

python manage.py show_urls

Session

Clear Django Session
manage.py clearsessions
show django sessions
(.venv) ~] manage.py dumpdata sessions --indent 4
[
{
    "model": "sessions.session",
    "pk": "81x3wqjjvfz3xfqfdbiqwstvyhzgnmw2",
    "fields": {
        "session_data": ".eJxVjMsOwiAQRf-FtSHA8KpL934DGWCQqqFJaVfGf1eSLnR7zrn3xQLuWw17pzXMmZ2ZZKdfFjE9qA2R79huC09L29Y58pHww3Z-XTI9L0f7d1Cx17HOPntnwEiSRUqHgjxATJOmL0lFaSsmSlIkA8paVYpA8sZZBGcMaPb-ANpvNz8:1t0j08:vYNI3VnFKEHC83kdaHwnqwJA3heTO0RVL3OXXEbREHA",
        "expire_date": "2024-10-29T14:58:20.345Z"
    }
},
{
    "model": "sessions.session",
    "pk": "dehk03zy29bmpkqv1jnz1yhi91hmlnjl",
    "fields": {
        "session_data": ".eJxVjMsOwiAQRf-FtSHA8KpL934DGWCQqqFJaVfGf1eSLnR7zrn3xQLuWw17pzXMmZ2ZZKdfFjE9qA2R79huC09L29Y58pHww3Z-XTI9L0f7d1Cx17HOPntnwEiSRUqHgjxATJOmL0lFaSsmSlIkA8paVYpA8sZZBGcMaPb-ANpvNz8:1t0gim:PktfVg2EBrfh-nJn6_xZNxOmZCtiefVKn5SdzlXfSpY",
        "expire_date": "2024-10-29T12:32:16.035Z"
    }
}
]

Django dump/load data

django dump data

dumpdata

manage.py dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]
  • Outputs to standard output all data in the database associated with the named application(s). If no application name is provided, all installed applications will be dumped.
  • The output of dumpdata can be used as input for loaddata
  • When result of dumpdata is saved as a file, it can serve as a fixture for tests or as an initial data .

example:

manage.py dumpdata --all --format=json --indent=4 app_ad > backup_data\app_ad.json

Save all data from app_ad app to backup_data folder and app_ad.json file

--all, -a
Uses Django’s base manager, dumping records which might otherwise be filtered or modified by a custom manager.
--format FORMAT
Specifies the serialization format of the output. Defaults to JSON. Supported formats are listed in Serialization formats .
--indent INDENT
Specifies the number of indentation spaces to use in the output. Defaults to None which displays all data on single line.
--exclude EXCLUDE, -e EXCLUDE
Prevents specific applications or models (specified in the form of app_label.ModelName) from being dumped. If you specify a model name, then only that model will be excluded, rather than the entire application. You can also mix application names and model names.
If you want to exclude multiple applications, pass --exclude more than once:
django-admin dumpdata --exclude=auth --exclude=contenttypes
--database DATABASE
Specifies the database from which data will be dumped. Defaults to default.

Problem with utf-8

If you've ever had an issue where certain characters weren't getting dumped correctly from your Django database, this one's for you:

python -Xutf8 manage.py dumpdata --all --format=json --indent=4 app_ad  > backup_data\app_ad_utf8.json

django load data

loaddata

manage.py loaddata fixture [fixture ...]

example:

manage.py loaddata --format=json --app=app_ad backup_data\app_ad.json

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus