pythondjangodjango-commands

How to specify a parameter of type Array into a Django Command?


It is straight forward creating a string parameter such as --test_email_address below.

   class Command(BaseCommand):
        option_list = BaseCommand.option_list + (
            make_option('--test_email_address',
                        action='store',
                        type="string",
                        dest='test_email_address',
                        help="Specifies test email address."),
            make_option('--vpds',
                        action='store',
                        type='list',           /// ??? Throws exception
                        dest='vpds',
                        help="vpds list [,]"),
        )

But how can I define a list to be passed in? such as [1, 3, 5]


Solution

  • You should add a default value and change the action to 'append':

    make_option('--vpds',
                action='append',
                default=[],
                dest='vpds',
                help="vpds list [,]"),
    

    The usage is as follows:

    python manage.py my_command --vpds arg1 --vpds arg2