Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created May 18, 2012 10:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igniteflow/2724472 to your computer and use it in GitHub Desktop.
Save igniteflow/2724472 to your computer and use it in GitHub Desktop.
Django management command with modified handle to dynamically call functions by name
class DynamicCommand(BaseCommand):
"""
When an arg is given after the management command this modified handle will automatically look for
and call a method with that name. Therefore, you can extend your management command Command() class
from this class instead of Command and simple write functions that will be called by
./manage.py your_file_name your_function_name
"""
def handle(self, *args, **kwargs):
argument = args[0]
extra_args = args[1:]
if hasattr(self, argument):
class_method = getattr(self, argument)
class_method(extra_args) if extra_args else class_method()
else:
logging.error("No methods found that match the given args")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment