See below example:
{{{
# management/commands/callme.py
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('poll_id', nargs='+', type=int)
def handle(self, *args, **options):
for poll_id in options['poll_id']:
self.stdout.write('%d' % poll_id)
# python manage.py shell
>>> from django.core.management import call_command
>>> call_command('callme', 1, 2, 3)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/mrkschan/tmp/tcallcommand/env/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 109, in call_command
defaults = parser.parse_args(args=args)
File "/home/mrkschan/tmp/tcallcommand/env/local/lib/python2.7/site-
packages/django/core/management/base.py", line 64, in parse_args
return super(CommandParser, self).parse_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1690, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1722, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1763, in _parse_known_args
option_tuple = self._parse_optional(arg_string)
File "/usr/lib/python2.7/argparse.py", line 2050, in _parse_optional
if not arg_string[0] in self.prefix_chars:
TypeError: 'int' object has no attribute '__getitem__'
>>> call_command('callme', '1', '2', '3')
1
2
3
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25483>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* component: Documentation => Core (Management commands)
* needs_tests: => 0
* version: 1.8 => master
* needs_docs: => 0
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
I think we could force all `args` to text before passing them to
`parse_args`.
--
Ticket URL: <https://code.djangoproject.com/ticket/25483#comment:1>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/25483#comment:2>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/25483#comment:3>
--
Ticket URL: <https://code.djangoproject.com/ticket/25483#comment:4>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"3f22e83e90bc2eeea5f65858660385a34fbf5486" 3f22e83]:
{{{
#!CommitTicketReference repository=""
revision="3f22e83e90bc2eeea5f65858660385a34fbf5486"
Fixed #25483 -- Allowed passing non-string arguments to call_command
Thanks KS Chan for the report and Tim Graham for the review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25483#comment:5>