2015-02-03 10 views
8

Wykonałem polecenie niestandardowe w django, aby usunąć zestaw kont w CMD, ale chcę móc uruchomić plik Pythona i poprostu poprosić o wiersz konta, aby usunąć numer konta, a następnie go usunąć. To jest to, co do tej pory miałem.Zażądaj danych wejściowych z niestandardowego polecenia django?

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) 

Odpowiedz

9

pomocą wbudowanego raw_input() funkcję:

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: 
     ... 
+3

'input()' w Pythonie 3 http://stackoverflow.com/questions/954834/how-do-i-use-raw -input-in-python-3 –

Powiązane problemy