请求来自django custom命令的输入?

Jab*_*ahs 10 python django

我在django中创建了一个自定义命令来删除CMD中的一个设置帐户,但是我希望能够运行python文件并让命令行询问我要删除的帐号然后删除它.这就是我到目前为止所拥有的.

from django.core.management.base import BaseCommand, CommandError
from accounts.models import client

class Command(BaseCommand):

    args = '<client_id client_id ...>'
    help = 'Closes the specified account.'

    def handle(self, *args, **options):
        for client_id in args:
            try:
                x = client.objects.get(pk=int(client_id))
            except client.DoesNotExist:
                raise CommandError('Client "%s" does no exist' % client_id)

            x.delete()

            self.stdout.write('Successfully closed account "%s"' % client_id)
Run Code Online (Sandbox Code Playgroud)

cat*_*ran 13

使用内置的raw_input()函数:

def handle(self, *args, **options):
    if args:
        ids = args
    else:
        ids = raw_input('Enter comma-delimited list of ids: ').split(',')
    for client_id in ids:
        ...
Run Code Online (Sandbox Code Playgroud)

  • Python 3中的`input()`http://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3 (6认同)