Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I perform Django's `syncdb --noinput` with call_command?

>>> 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.

like image 690
pvoosten Avatar asked Sep 02 '11 07:09

pvoosten


People also ask

What is Django-admin and manage py and explain its commands?

Manage.py in Django is a command-line utility that works similar to the django-admin command. The difference is that it points towards the project's settings.py file. This manage.py utility provides various commands that you must have while working with Django.

What is the use of manage py in Django?

It is your tool for executing many Django-specific tasks -- starting a new app within a project, running the development server, running your tests... It is also an extension point where you can access custom commands you write yourself that are specific to your apps.

Where is manage py located?

The django-admin.py script should be on your system path if you installed Django via its setup.py utility. If it's not on your path, you can find it in site-packages/django/bin within your Python installation.


1 Answers

call_command('syncdb', interactive = False) 

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).py

The python module where the syncdb command resides is django.core.management.commands.syncdb

To find the source code of the command you can do something like this:

(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' >>>  

Of course, check the contents of syncdb.py, and not syncdb.pyc.

Or looking at the online source, the syncdb.py script contains:

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

that tells us that instead of --noinput on the command line, we should use interactive if we want to automate commands with the call_command function.

like image 55
pvoosten Avatar answered Sep 21 '22 17:09

pvoosten