2011-09-02 3 views
24
>>> from django.core.management import call_command 
>>> call_command('syncdb') 

은 python 스크립트 내에서 syncdb 관리 명령을 실행합니다. 그러나 파이썬 쉘 또는 스크립트 내에서Django의`syncdb --noinput`을 call_command로 어떻게 수행 할 수 있습니까?

$ python manage.py syncdb --noinput 

을 실행하고 싶습니다. 어떻게해야합니까?

슈퍼 유저를 만들지 묻지 않고 다음 라인들은 작동하지 않습니다.

>>> call_command('syncdb', noinput = True) # asks for input 
>>> call_command('syncdb', 'noinput') # raises an exception 

나는 장고 1.3을 사용합니다.

+2

해결 :'call_command ('syncdb', interactive = False)' – pvoosten

+0

[이 질문은 중복되었습니다 (http://stackoverflow.com/questions/2772990/programmatically-sync-the-db-in-django)? –

+2

@lbp 주석에 솔루션을 삽입하지 말고 대답으로 받아 들여야합니다. 그렇지 않으면이 질문은 영원히 받아 들여지지 않는 대답이 될 것입니다. –

답변

42
call_command('syncdb', interactive = False) 

편집 :

나는 소스 코드에 대한 답을 발견했다. 모든 관리 명령에 대한 소스 코드는 syncdb 명령이있는 management/commands/(command_name).py

파이썬 모듈라는 파이썬 모듈에서 발견 될 수있다 django.core.management.commands.syncdb

당신이 뭔가를 할 수있는 명령의 소스 코드를 찾을 수 있습니다 :

(env)$ ./manage.py shell 
>>> from django.core.management.commands import syncdb 
>>> syncdb.__file__ 
'/home/user/env/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.pyc' 
>>> 

물론 syncdb.py가 아닌 syncdb.py의 내용을 확인하십시오.

또는 online source에서 찾고 syncdb.py 스크립트를 포함하는 대신 명령 행에서 --noinput의 우리가 call_command 기능 명령을 자동화하려는 경우, 우리가 interactive를 사용하는 것을 우리에게 알려줍니다

make_option('--noinput', action='store_false', dest='interactive', default=True, 
     help='Tells Django to NOT prompt the user for input of any kind.'), 

합니다.

+0

'noinput' 대신'interactive'를 사용하는 이유를 아는 사람이 있습니까? 이것은 장고 문서 어디서나 있습니까? –

+0

내가 알 수있는 한,이 주제에 대한 문서는 매우 모호합니다. 여기에 예제가 있습니다 : https://docs.djangoproject.com/en/dev/ref/django-admin/#running-management-commands-from-your-code – pvoosten

+0

답변을 많이 주셔서 감사합니다 – ihatecache

관련 문제