Dec. 17, 2024

Django Management Commands in a 100 words

Management commands to me are a core interaction with any Django project. They are the command line interface to acting on a Django project in a manner, this ranges from a development server, moving files around, dealing with migrations or helping with debugging using a python shell, and this is just the commands that come with Django core.

Management commands really come into their own when you add them to your project. I have used them to setup a blank project with seed data, migrated data in a controlled manner (ie not during a deployment) and that is just in the last couple of weeks. Finally when combined with *nix cron they are a simple way to get code running at regular intervals, which is perfect for polling an external API and updating or database, sending out emails (or other notifications) or extracting data from your application to another location.

Writing a command is covered in the docs in depth, however it's simple to get started, the below code gets put in the folder management/commands/<my_command_name>.py

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = "Some help text"

    def handle(self, *args, **options):
        

That's it! The command is then available at manage.py <command_name>.