A quick-and-dirty, and extremely simple, decorator to turn a simple function into a management command.
This still requires you to have the management directory structure, but allows you to name your primary method whatever you want, and encapsulates the basic functionality of an argument-accepting management commmand.
The function's docstring will be used for the command's help text if the
help arg is not passed to the decorator.
from myapp.utils import command @command() def my_command(): print "Hello, world"
I'm not too familiar with the intricacies of decorators and management commands, so this could probably (most likely) be improved upon, but it's a start.
Update: I've taken this a bit farther and put my work up on bitbucket: https://bitbucket.org/eternicode/django-management-decorators/src
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from django.core.management.base import BaseCommand def command(help=None, args=''): def _decorate(func): _args = args _help = help class Command(BaseCommand): help = _help if _help is not None else func.__doc__ args = _args def handle(self, *args, **kwargs): return func(*args, **kwargs) func.func_globals['Command'] = Command return func return _decorate
More like this
- Add custom fields to the built-in Group model by jmoppel 1 month, 1 week ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 4 months, 3 weeks ago
- Python Django CRUD Example Tutorial by tuts_station 5 months, 1 week ago
- Browser-native date input field by kytta 6 months, 3 weeks ago
- Generate and render HTML Table by LLyaudet 7 months ago