How does one use Django custom management command option?

前端 未结 2 1639
故里飘歌
故里飘歌 2021-01-31 11: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):         


        
2条回答
  •  孤城傲影
    2021-01-31 11:55

    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)

提交回复
热议问题