This is my Django Commands Cheat Sheet.
This cheat sheet has useful commands that you can use with your django project.
Here are the commands:
Load database from json file
python manage.py loaddata db.json
This is really useful for restoring your local database from a json file. The next command will show you how to dump your database to such a file.
Dump database to json file
python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
You may be wondering why I include –exclude auth.permission and –exclude contenttypes.
When you don’t remove these tables, django will complain about an integrity error. Most of the time I want to restore my database from the json file, and in order to do so, I exclude these tables.
Create Super User
python manage.py createsuperuser
This is just a simple command to create a django super user. You can then use this created user to sign into the django admin.
Launch Django Shell
python manage.py shell
This will launch a shell with the django context. You can use this shell to access your django models and query your database with the django orm.
python manage.py makemigrations
This command will create migration files based on your model changes that django will use to modify the database.
Making Named Migrations
python manage.py makemigrations --name <migration_name>
This command will create a named migration. I like using this to give my migrations descriptive names that demonstrate what the migration does.
Show Applied Migrations
python manage.py showmigrations
With this you django will show you which migrations have been applied to your database. Django stores this data in a migrations table, and that is where the command is pulling this data from.
Fake a Migration
python manage.py migrate <app> <migration>
This is really useful when you are having trouble applying a migration due to a column already existing on a model or some other type of conflict. if you run this command, django will mark this migration as applied in the migrations table, without actually running any SQL.
That’s all for now.
If you are interested in django, maybe you are interested in this article about How To Upgrade To Django 2. Please check it out.
I will continue to upgrade this cheat sheet with more django commands overtime so please check back.
Have a great day!