问题描述
我正在django中创建自定义用户,因为我在初始迁移文件中编写了以下代码,但是当我们点击migration命令时,它会给出TransactionManagementError
0001_initial.py
from django.db import migrations
from api.user.models import CustomUser
class Migration(migrations.Migration):
def seed_data(apps,schema_editor):
user = CustomUser(name = "abc",email = "abc@gmail.com",is_staff = True,is_superuser = True,phone = "9874561230",gender = "Male")
user.set_password("12345")
user.save()
dependencies = [
]
operations = [
migrations.RunPython(seed_data),]
发生错误:
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You
can't execute queries until the end of the 'atomic' block.
解决方法
为什么不使用fixtures。这样,您可以在确定模式已迁移到数据库之后通过加载固定装置来添加种子数据。
,将重复此问题。请尝试此参考。
参考: Can't execute queries until end of atomic block in my data migration on django 1.7