Created
May 18, 2012 10:18
-
-
Save igniteflow/2724472 to your computer and use it in GitHub Desktop.
Django management command with modified handle to dynamically call functions by name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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