How to make syncdb work automatically in Django?
When I have a view that helps in uploading of a file , I want it to be stored in the database , which is only possible if I run syncdb . But , 开发者_StackOverflow中文版till now I know how to run python manage.py syncdb at the python command line . How do I make it run automatically when I upload some data ?
A little bit hacky way:
import sys
sys.argv.append('syncdb')
from django.core.management import execute_manager
import settings # Your project's settings. Assumed to be in the same directory.
execute_manager(settings)
You can always call
os.system('python /path/manage.py syncdb')
This should run your syncdb process, however, I think there are better ways to solve this without using syncdb at all.
Oh, and you will need to use flag that going to answer y to all questions.
Calling syncdb
on every file upload is wrong per se, but answering your question. To call syncdb
from inside code:
from django.core.management.commands.syncdb import Command as SyncDbCommand
SyncDbCommand().handle_noargs()
Django has a official way of Running management commands from your code
精彩评论