Hick
Hick

Reputation: 36404

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 , 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 ?

Upvotes: 1

Views: 676

Answers (4)

outcoldman
outcoldman

Reputation: 11832

Django has a official way of Running management commands from your code

Upvotes: 1

Jerzyk
Jerzyk

Reputation: 3742

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

Upvotes: 1

Zaur Nasibov
Zaur Nasibov

Reputation: 22659

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)    

Upvotes: 3

nagisa
nagisa

Reputation: 720

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.

Upvotes: 1

Related Questions