Skip to content

Instantly share code, notes, and snippets.

@calumroy
Last active August 29, 2015 14:03
Show Gist options
  • Save calumroy/de903e3a640344a8817d to your computer and use it in GitHub Desktop.
Save calumroy/de903e3a640344a8817d to your computer and use it in GitHub Desktop.
Management Commands Django

Creating a Management Command in Django

  • Create a management and commands directory in the Django project
    mkdir management cd management mkdir commands

  • Create the empty init.py nano __init__.py
    cd commands nano __init__.py

  • Create a new command in the commands directory
    nano newCommand.py

  • Add the following to newCommand.py

    from django.core.management.base import BaseCommand, CommandError  

    class Command(BaseCommand):
      args = '<command_input>'
      help = 'specify help for the command'
      def handle(self, *args, **options):
        ...

Each Python module in the commands directory will be auto-discovered and registered as a command that can be executed as an action when you run manage.py.

Each newCommand must extend Djangos BaseCommand or one of its subclasses and must reimplement the handle function (this is where the logic of the command is defined).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment