Management Command Aliases

A quick minor efficiency tip for today that'll make your life at the Django command line incrementally better.

You're using django-extensions in your project, right? If not, go and rectify that problem immediately.

The best feature™ that django-extensions provides is the shell_plus command, which imports all of your models and the most common django modules. If you've got ipython installed (you should!) the shell will launch within an ipython shell.

shell plus terminal

You know the worst thing about shell_plus? The name. That's a long name to type out into your shell, especially since it's snake_case, and I type the command approximately 15,000 times per day.

Wouldn't it be nice to alias shell_plus to something like sh? Well, now you can!

Create a new file under one of your apps (I chose the "core" app) commands subfolders called sh.py and add the following code:

# core/management/commands/sh.py
"""
A shortcut for shell_plus
"""
from django_extensions.management.commands.shell_plus import Command

And now you too can enter a shell_plus session with the much nicer command sh.

$ ./manage.py sh

Why not a bash/zsh/whatever alias?

Because docker, really. We could mount everyones bash or zsh profile. Or we could just rename a command and still access all of the help, arguments, and command line switches previously available. I like that.

Bonus Section

Setup your own extra default imports, so you'll never have to import timedelta ever again!

SHELL_PLUS_POST_IMPORTS = [
    ("sales.enums", "*"), 
    ("datetime", "timedelta")
]