50

I need to pass in an integer argument to a base command in Django.

For instance, if my code is:

from django.core.management import BaseCommand
class Command(BaseCommand):
 def handle(self, *args, **options, number_argument):
 square = number_argument ** 2
 print(square)

I want to run:

python manage.py square_command 4

so, it will return 16.

Is there a way I can pass an argument through the terminal to the command I want to run?

Penny Liu
18k5 gold badges89 silver badges109 bronze badges
asked Dec 22, 2014 at 22:35
0

4 Answers 4

96

Add this method to your Command class:

def add_arguments(self, parser):
 parser.add_argument('my_int_argument', type=int)

You can then use your option in the code, like this:

def handle(self, *args, **options):
 my_int_argument = options['my_int_argument']

The benefit of doing it this way is that the help output is automatically generated for manage.py my_command --help

Rob Bednark
28.8k28 gold badges90 silver badges131 bronze badges
answered Jan 25, 2016 at 12:29
Sign up to request clarification or add additional context in comments.

6 Comments

As of Django 2.8, this is the way to do it.
How can you add default value?
what is the purpose of *args then?
it's a standard pattern in Python to add *args and **kwargs: it ensures that when people inherit from this class you still can add extra keyword args in the function you override.
|
3

Yes. The mechanism for doing so is described here, but basically, you can get the argument from args[0].

SukiCZ
3907 silver badges12 bronze badges
answered Dec 22, 2014 at 22:38

2 Comments

Found a more complete answer here: stackoverflow.com/questions/10568864/…
@PeterGraham now you should use the add_arguments method, which was added in version 1.8, so I wouldn't recommend that more complete answer anymore.
2

This is pretty simple by implementing the add_arguments method. Then, you can get the value of this argument in the handle method as follows:

class Command(BaseCommand):
 help = 'Help for command'
 def add_arguments(self, parser):
 parser.add_argument('n', type=int, help='help for n')
 def handle(self, *args, **options):
 n = options['n']
 square = n ** 2
 print(square)

The parameter parser is an instance of argparse.ArgumentParser (see the docs). Now you can add as many arguments as you want by calling parser's add_argument method. In the code above, you are expecting a parameter n of type int which is gotten in the handle method from options.

If you want to see the help displayed, run the following:

python manage.py square_command --help

That will produce an output similar to the following one:

usage: manage.py create_documents [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
 [--skip-checks]
 n
Help for command
positional arguments:
 n help for n
answered Jul 25, 2020 at 23:52

Comments

1

django-typer allows this to be specified more concisely than the BaseCommand class which uses argparse. The above could be rewritten:

from django_typer import TyperCommand
class Command(TyperCommand):
 help = 'Help for command'
 def handle(self, n: int):
 print(n ** 2)
answered Mar 5, 2024 at 20:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.