5 Django Commands You NEED to Know to Administer Your App
As a robust and flexible framework, Django equips developers with a ton of commands to streamline web application development and administration. While commands like runserver
, migrate
, and createsuperuser
are frequently used, Django offers a wealth of lesser-known commands that can significantly enhance your workflow and efficiency. In this article, we take a look at five Django commands that can help you administer your application with greater ease and precision. Whether you're a seasoned Django developer or just starting out, these hidden gems will provide valuable insights and tools to better manage your projects.
diffsettings
diffsettings
displays differences between your current settings.py
and Django’s default settings. This is great for when you’re debugging configuration issues, as it shows exactly what has been overridden and what remains the default value. As a bonus, this is helpful for your documentation since you can quickly generate a list of all your app’s custom settings.
Syntax
To use the diffsettings
command, you run the following command:
python manage.py diffsettings
Output
The output of this command will look something like this, with all of your custom settings listed:
ALLOWED_HOSTS = ['example.com']
DEBUG = False
LANGUAGE_CODE = 'fr'
Pro tip: Add --output unified
to the end of the command to get an output that looks more like a typical git diff.
Read more about diffsettings
in the Django docs.
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.
Syntax
To use the inspectdb
command, you run the following command:
python manage.py inspectdb
Output
The 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.
showmigrations
showmigrations
displays all of your project’s migrations and whether or not they have been applied. This is useful for when you aren’t quite sure where things stand and don’t want to blindly apply your migrations. Use this command for tracking your migrations, debugging, documentation, and deployment verification.
Syntax
To use the showmigrations
command, you run the following command:
python manage.py showmigrations
Output
The output of this command will look something like this:
app_name
[ ] 0001_initial
[X] 0002_auto_20210516_1234
[ ] 0003_auto_20210610_1432
[]
indicates that the migration has not been applied where [x]
indicates that the migration has been applied.
Pro tip: Add the --plan
flag to the command to simulate how Django will apply your migrations. This may save you if you have some tricky migration logic.
Read more about showmigrations
in the Django docs.
dbshell
dbshell
provides an interactive shell for the database engine you specified in your ENGINE
settings. By default this is SQLite but you can choose from a different engine. For example, PostgreSQL would use psql
and MySQL would use mysql
.
Syntax
To use the dbshell
command, you run the following command:
python manage.py dbshell
Output
The output of this command will look something like this, depending on which engine you use:
SQLite version 3.43.2 2023-10-10 13:08:14
Enter ".help" for usage hints.
sqlite>
Pro tip: If you have multiple databases you can use the --database
flag to choose which one you want to connec to.
Read more about dbshell
in the Django docs.
dumpdata
dumpdata
outputs all of the data in your database to a file. This is fantastic for backing up your database to a file when you might be making some risky changes or if you want to migrate the data to another project. It can also be great for populating a local testing environment so that you don’t have to start fresh.
Syntax
To use the dumpdata
command, you run the following command:
python manage.py dumpdata
Output
For a simple model like this:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
The output of this command will look something like this:
[
{
"model": "myapp.author",
"pk": 1,
"fields": {
"name": "John Doe",
"age": 35
}
},
{
"model": "myapp.author",
"pk": 2,
"fields": {
"name": "Jane Smith",
"age": 28
}
}
]
Pro tip: Use the --output
flag to specify a custom name for your db backup file.
Read more about dumpdata
in the Django docs.
Conclusion
In conclusion, while Django’s well-known commands like runserver
, migrate
, and createsuperuser
are essential for everyday development, exploring these lesser-known commands can greatly enhance your productivity and efficiency. Commands like diffsettings
, inspectdb
, showmigrations
, dbshell
, and dumpdata
offer powerful functionalities that simplify debugging, database management, and project maintenance. By incorporating these tools into your workflow, you can manage your Django applications with greater precision and ease. Play around with these commands and see how they can fit into your development toolkit to make your Django projects more manageable and robust.
Say hi on Twitter and let us know how you’re using these commands!