>>> from django.core.management import call_command
>>> call_command('syncdb')
executes the syncdb management command from within a python script. However, I want to run the equivalent of
$ python manage.py syncdb --noinput
from within a python shell or script. How can I do that?
The following lines don’t work without interrupting me with the question whether I want to create a super user.
>>> call_command('syncdb', noinput = True) # asks for input
>>> call_command('syncdb', 'noinput') # raises an exception
I use Django 1.3.
EDIT:
I found the answer in the source code. The source code for all management commands can be found in a python module called
management/commands/(command_name).pyThe python module where the
syncdbcommand resides isdjango.core.management.commands.syncdbTo find the source code of the command you can do something like this:
Of course, check the contents of syncdb.py, and not syncdb.pyc.
Or looking at the online source, the
syncdb.pyscript contains:that tells us that instead of
--noinputon the command line, we should useinteractiveif we want to automate commands with thecall_commandfunction.