Question

I'm working on a project that includes a django server, and also a setup module.

The user will be configuring their system to run my program, which includes a django webserver element along with other items. I'm working on a setup module that assists the user in getting all of the settings correct and sets up all of the appropriate files. One of the things that I'd like to be during the setup process is essentially a "manage.py syncdb" command that creates an appropriate SQLite file and table from nothing.

I could grab the code found in manage.py and directly stick it into my setup module appropriately, but I'm not sure if there's a better approach that I'm missing - along the lines of two lines consisting of:

import django.something
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.something.syncdb()

Or something of the sort. Am I just missing something here?

Was it helpful?

Solution

This should do it:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core import management
management.call_command('syncdb', interactive=False)

You can also do

import os
import settings
from django.core.management.commands import syncdb 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

syncdb.Command().execute(noinput=True) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top