Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically sync the db in Django

I'm trying to sync my db from a view, something like this:

from django import http
from django.core import management

def syncdb(request):
    management.call_command('syncdb')
    return http.HttpResponse('Database synced.')

The issue is, it will block the dev server by asking for user input from the terminal. How can I pass it the '--noinput' option to prevent asking me anything?

I have other ways of marking users as super-user, so there's no need for the user input, but I really need to call syncdb (and flush) programmatically, without logging on to the server via ssh. Any help is appreciated.

like image 874
Attila O. Avatar asked May 05 '10 12:05

Attila O.


People also ask

What is Runserver in Django?

The runserver command is a built-in subcommand of Django's manage.py file that will start up a development server for this specific Django project.

How do I use Django Inspectdb?

Django Inspectdb Refactor will automatically create the required folders and create separate python files for each model. You can install it via pip or to get the latest version clone this repo. Add ``inspectdb_refactor`` to your ``INSTALLED_APPS`` inside settings.py of your project. make models in that particular app.

How do I use fixtures in Django?

Providing data with fixtures The most straightforward way of creating a fixture if you've already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML (with PyYAML installed) documents.


2 Answers

management.call_command('syncdb', interactive=False)
like image 77
Davor Lucic Avatar answered Oct 13 '22 21:10

Davor Lucic


Works like this (at least with Django 1.1.):

from django.core.management.commands import syncdb
syncdb.Command().execute(noinput=True)
like image 38
maersu Avatar answered Oct 13 '22 19:10

maersu