[Answered ]-Django custom management command with variable length of args list

2πŸ‘

βœ…

It’s not possible to do this automatically. But you can work with a different action type:

option_list = BaseCommand.option_list + (
    make_option(
        "-i",
        "--user_id",
        dest="user_id",
    ),
    make_option(
        "-f",
        "--fields",
        dest="fields",
        action="append",
    ),
)

Now supply multiple field names at the command line like so:

./my_program -u my_user -f field1 -f field2 -f field3

options['fields'] will then contain a list of field names or None. You can check for this (and the fact that the list is longer than five elements) and print an error message.

πŸ‘€sjaensch

Leave a comment