我编写了一个数据迁移文件,用于为MPTT模型创建一些初始数据。
但创作失败了,
def create_foo(apps, schema_editor):
db_alias = schema_editor.connection.alias
logger = logging.getLogger(__name__)
Foo = apps.get_model("app", "Foo")
for attr in ROLES:
try:
foo = Foo.objects.using(db_alias).get(name=attr[0])
except Foo.DoesNotExist:
parent = Foo.objects.using(db_alias).get(name=attr[1]) if attr[1] else None
if parent:
foo = Foo.objects.create(name=attr[0], parent=parent)
else:
foo = Foo.objects.using(db_alias).create(name=attr[0])
logger.info("Created foo - %s" % foo)
我在执行以下行时遇到错误,有什么想法吗?
foo = Foo.objects.using(db_alias).create(name=attr[0])
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py”,第36行,在defaulterrorhandler中 提出错误类,错误值 django.db.utils.IntegrityError:(1048,“Column'ft''不能为空”)
答案 0 :(得分:4)
您必须使用mptt:
手动注册模型from mptt import register
def create_foo(apps, schema_editor):
db_alias = schema_editor.connection.alias
logger = logging.getLogger(__name__)
Foo = apps.get_model("app", "Foo")
register(Foo)
# ... rest of your code...