Categories
Python

Django Friday Tips: Less known builtin commands

Django management commands can be very helpful while developing your application or website, we are very used to runserver, makemigrations, migrate, shell and others. Third party packages often provide extra commands and you can easily add new commands to your own apps.

Today lets take a look at some less known and yet very useful commands that Django provides out of the box.


diffsettings

$ python manage.py diffsettings --default path.to.module --output unified

Dealing with multiple environments and debugging their differences is not as rare as we would like. In that particular scenario diffsettings can become quite handy.

Basically, it displays the differences between the current configuration and another settings file. The default settings are used if a module is not provided.

- DEBUG = False
+ DEBUG = True
- EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
+ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
- TIME_ZONE = 'America/Chicago'
+ TIME_ZONE = 'UTC'
+ USE_SRI = True
- USE_TZ = False
+ USE_TZ = True
...

sendtestemail

$ python manage.py sendtestemail my@address.com

This one does not require an extensive explanation. It lets you test and debug your email configuration by using it to send the following message:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Test email from host on 2022-04-28 19:08:56.968492+00:00
From: webmaster@localhost
To: my@address.com
Date: Thu, 28 Apr 2022 19:08:56 -0000
Message-ID: <165117293696.405310.3477251481753991809@host>

If you're reading this, it was successful.
-----------------------------------------------------------

inspectdb

$ python manage.py inspectdb

If you are building your project on top of an existing database (managed by other system), inspectdb can look into the schema and generate the respective models for Django’s ORM, making it very easy to start using the data right away. Here’s an example:

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.

...

class AuthPermission(models.Model):
    content_type = models.ForeignKey('DjangoContentType', models.DO_NOTHING)
    codename = models.CharField(max_length=100)
    name = models.CharField(max_length=255)

    class Meta:
        managed = False
        db_table = 'auth_permission'
        unique_together = (('content_type', 'codename'),)
...

showmigrations

$ python manage.py showmigrations --verbosity 2

When you need to inspect the current state of the project’s migrations in a given environment the above command is the easiest way to get that information. It will tell you what migrations exist, which ones were applied and when.

admin
 [X] 0001_initial (applied at 2021-01-13 19:49:24)
 [X] 0002_logentry_remove_auto_add (applied at 2021-01-13 19:49:24)
 [X] 0003_logentry_add_action_flag_choices (applied at 2021-01-13 19:49:24)
auth
 [X] 0001_initial (applied at 2021-01-13 19:49:24)
 [X] 0002_alter_permission_name_max_length (applied at 2021-01-13 19:49:24)
 [X] 0003_alter_user_email_max_length (applied at 2021-01-13 19:49:24)
...

There are many other useful management commands that are missing in the base Django package, to fill that gap there are some external packages available such as django-extensions. But I will leave those to a future post.

By Gonçalo Valério

Software developer and owner of this blog. More in the "about" page.

One reply on “Django Friday Tips: Less known builtin commands”

Very nice.
This is interesting – I like those litte tricks 🙂

Just stumbled across yoru blog post, it was suggested in ‘Python Weekly’.

Leave a Reply

Your email address will not be published.