如何在Django 1.7中首次迁移后将初始值插入到数据库表中。
这就是我的尝试。
的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())]
还有我如何处理外键字段?
答案 0 :(得分:0)
我认为您的迁移文件应该命名为
myproject/myapp/migrations/0002_sql_fixtures.py