1
如何在Django 1.7中首次遷移後將初始值插入到我的數據庫表中。SQL夾具配置
這是我試過的。
的myproject> MYAPP> SQL> myModel.sql
INSERT INTO myapp_myModel (first_name, last_name) VALUES ('John', 'Smith');
的myproject>的myapp> models.py
class MyModel(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
的myproject> MYAPP>遷移> init.py
from __future__ import unicode_literals
from django.db import models, migrations
def load_stores_from_sql():
from myproject.settings import BASE_DIR
import os
sql_statements = open(os.path.join(BASE_DIR, 'myapp/sql/myModel.sql'), 'r').read()
return sql_statements
class Migration(migrations.Migration):
dependencies = [
('myapp', '0001_initial'),
]
operations = [migrations.RunSQL(load_stores_from_sql())]
,以及如何去有關外鍵字段?
非常感謝,我嘗試過了,它似乎是工作得很好。 – phourxx 2015-03-13 14:39:29