0

テーブルに初期データを入力する方法。?? 私はこれを読みました: [1] https://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-sql-data

直接木:

    |-- 管理.py
    |-- mydb
    |-- QAapp
    | | |-- __init__.py
    | | |-- __init__.pyc
    | | |-- 移行
    | | | | |-- 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
    | | | | `--スクラップ.py
    | | |-- テスト.py
    | | `-- ビュー.py
    `-- QAサイト
        |-- __init__.py
        |-- __init__.pyc
        |-- 設定.py
        |-- 設定.pyc
        |-- urls.py
        ` -- wsgi.py

モデルファイル:

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 が含まれています

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

問題は:manage.pyに何かを追加する必要がありますか[または]シェルからいくつかのコマンドを実行します[または]これらの挿入クエリをテーブルに入力するために他のことをしますか??

私は試しました:
python manage.py sqlall
python manage.py syncdb

4

1 に答える 1

0

はい、実行する必要があります:

python manage.py syncdb

これにより、すべてのテーブルが作成され、SQL スクリプトが実行されます。

アップデート:

サウスを使用しているため、syncdb を制御して移行を提供するサウスは、テーブルを作成した後に syncdb を模倣し、sql ファイルをロードするようです。DjangoSnippets でこのスニペットを見つけたので、南でも同じことができます。SQL ファイル名を変更する必要があるページの右側にある注意事項を考慮してください。このコードは 2011 年に投稿されましたが、これがまだ南に当てはまるかどうかはわかりません。

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

于 2013-09-07T21:24:00.253 に答える