0

I have read have read this documentation from Django on providing initial SQL data

direc tree :

    |-- manage.py
    |-- mydb
    |-- QAapp
    |   |-- __init__.py
    |   |-- __init__.pyc
    |   |-- migrations
    |   |   |-- 0001_initial.py
    |   |   |-- 0001_initial.pyc
    |   |   |-- 0002_auto__add_report__add_questioncontainer.py
    |   |   |-- 0002_auto__add_report__add_questioncontainer.pyc
    |   |   |-- __init__.py
    |   |   `-- __init__.pyc
    |   |-- models.py
    |   |-- models.pyc
    |   |-- sql
    |   |   |-- questioncontainer.sql
    |   |   `-- scrap.py
    |   |-- tests.py
    |   `-- views.py
    `-- QAsite
        |-- __init__.py
        |-- __init__.pyc
        |-- settings.py
        |-- settings.pyc
        |-- urls.py
        `-- wsgi.py

Model file :

from django.db import models
class QuestionContainer(models.Model):
    topic           = models.CharField(max_length=100)
    subtopic        = models.CharField(max_length=100)
    question        = models.TextField()
    options         = models.TextField()
    correct_answer  = models.CharField(max_length=50)
    total_attempts  = models.IntegerField()
    solved          = models.IntegerField()
    level           = models.IntegerField()

class Report(models.Model):
    name                = models.CharField(max_length=200)
    email               = models.EmailField()
    content             = models.TextField()
    QuestionContainer   = models.ForeignKey(QuestionContainer)

questioncontainer.sql contains

INSERT INTO QAapp_QuestionContainer(topic, subtopic, question, options, 
correct_answer, total_attempts, solved, level) VALUES ('general-aptitude', 'age-problems', 
'The Average age of a class of 22 students in 21 years. The average increases by 1
when the teachers age also included. What is the age of the teacher?', 
'A. 44 C. 41 B. 43 D. 40', '[A]', 0, 0, 0);

The question is, do I need to add something to manage.py [or] execute some command from shell [or] any other thing in order to populate table with those insert queries?

I have tried :
python manage.py sqlall
python manage.py syncdb

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
shifu
  • 6,586
  • 5
  • 21
  • 21

1 Answers1

0

Yes, you need to run:

python manage.py syncdb

which will create all tables and then run your sql script..

Update:

Since you use south, it seems that south takes control of syncdb and provides migrate instead does imitate syncdb and load sql files after creating tables. I found this snippet on DjangoSnippets which let you do the same thing with south. Take into consideration the notes on the right side of the page where you might need to change sql files names. This code posted on 2011 and I am not sure if this is still the case with south.

http://djangosnippets.org/snippets/2311/

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
almalki
  • 4,595
  • 26
  • 30