我在 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)
最佳答案
使用内置的 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:
...
编辑:在 Python3 中,使用 input()而不是 raw_input()
关于python - 从 django 自定义命令请求输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28301039/