How does one use Django custom management command option?

主宰稳场 提交于 2019-12-03 04:46:19

问题


The Django doc tell me how to add an option to my django custom management command, via an example:

from optparse import make_option

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--delete',
            action='store_true',
            dest='delete',
            default=False,
            help='Delete poll instead of closing it'),
    )

Then the docs just stop. How would one write the handle method for this class to check whether the user has supplied a --delete option? At times Django makes the easy things difficult :-(


回答1:


You can do it like this:

from optparse import make_option

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--del',
            action='store_true',
            help='Delete poll'),
        make_option('--close',
            action='store_true',
            help='Close poll'),
    )

    def handle(self, close, *args, **kwargs):
        del_ = kwargs.get('del')

Do note that some keywords in Python are reserved so you can handle those using **kwargs. Otherwise you can use normal arguments (like I did with close)




回答2:


A little suggestion about defining the commands (key name, dest) and handling default values (in make_option and in the command):

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--del',
            action='store_true',
            help='Delete all polls.',
            dest='your_name_for_delete',
            default=False),
        make_option('--close',
            action='store_true',
            help='Close all polls.'),
    )

    def handle(self, close, *args, **options):
        if options.get('your_name_for_delete'):
            Poll.objects.delete()
        if options.get('close', False):
            Poll.objects.update(closed=True)

In Django code you'll find "keyword arguments" (**kwargs) often named as **options, which is more suggestive (I stick to this naming convention).

The default value can be either specified in make_option, or through the dict.get method, which allows for a default value.

There's no reason not u use both defaults, should your Command.handle method be called manually, where the **options dictionary could be missing this entry.



来源:https://stackoverflow.com/questions/4208006/how-does-one-use-django-custom-management-command-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!