2

How do I enable a Django custom command to accept multiple arguments? Suppose that my command is to be called process_data, and in myproject/myapp/management/commands/process_data.py I have the following:

class Command(BaseCommand):
    def handle(self, *args, **options):
        alpha = args[0]
        beta = args[1]

And then call the command as follows:

>python manage.py process_data 5 16

I get the error:

manage.py: error: no such option: 16

This suggests that it sees my second argument 16 as part of **options, rather than a second argument in *args. How can I ensure that it is processed as an argument?

1 Answer 1

1

You don't add options to the management command like this. Check the Django documentation about writing management commands.

Rewriting your example without testing it (it might have typos):

from optparse import make_option
class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--alpha',
            action='store',
            dest='alpha'),
        make_option('--beta',
            action='store',
            dest='beta'),
        )

    def handle(self, *args, **options):
        alpha = options['alpha']:
        beta = options['beta']
        # ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.