如何运行自定义django-admin manage.py命令

时间:2017-03-24 12:32:12

标签: python django

我想用多个城市的id运行我的自定义命令。我该怎么办?我没有在文档中找到任何东西。这是我命令的源代码:

from django.core.management.base import BaseCommand, CommandError
from reservation.models import City

class Command(BaseCommand):
    help = 'Closes the specified poll for voting'

    def add_arguments(self, parser):
        parser.add_argument('city_id', nargs='+', type=int)

    def handle(self, *args, **options):
        for city_id in options['city_id']:
            try:
                city = City.objects.get(pk=city_id)
            except City.DoesNotExist:
                raise CommandError('City "%s" does not exist' % city_id)

            print city

此命令适用于python manage.py command_name 1。它打印id = 1的城市。但我想打印id为1,2的城市,而不是多次执行相同的命令。它是python manage.py command_name 1, 2python manage.py command_name [1,2,3]。这样的事情不起作用。

1 个答案:

答案 0 :(得分:1)

好吧,我刚刚意识到我应该执行例如

  

python manage.py command_name 1 2 3

打印ID为1,2和3的城市。