Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:无法为我的OneToOneField关系分配用户:问题_Python_Django - Fatal编程技术网

Python ValueError:无法为我的OneToOneField关系分配用户:问题

Python ValueError:无法为我的OneToOneField关系分配用户:问题,python,django,Python,Django,我在OneToOneField上遇到了一个非常奇怪的问题。我有一个非常简单的模型,比如 class Doctor(models.Model): user = models.OneToOneField(User) 问题在于我的迁移方法。我已经编写了一个0002_addusers迁移,它依赖于0001_initial,代码如下: class Migration(migrations.Migration): def create_users(apps, schema_editor):

我在OneToOneField上遇到了一个非常奇怪的问题。我有一个非常简单的模型,比如

class Doctor(models.Model):
    user = models.OneToOneField(User)
问题在于我的迁移方法。我已经编写了一个0002_addusers迁移,它依赖于0001_initial,代码如下:

class Migration(migrations.Migration):
    def create_users(apps, schema_editor):
        u = User.objects.create_superuser('admin', 'admin@aaa.com', 'admin')
        u.save()

        du = User.objects.create_user(username='doc01', password='doc01')
        du.save()

def create_doctors(apps, schema_editor):
    Doctor = apps.get_model('custom_user', 'Doctor')
    du = User.objects.get(username='doc01')

    d = Doctor(user=du)
    d.save()

dependencies = [
    ('custom_user', '0001_initial')
]

operations = [
    migrations.RunPython(create_users),
    migrations.RunPython(create_doctors),
]
对我来说,真正奇怪的是,这段非常简单的代码在视图中工作,在shell中工作,在任何地方都工作,除了在迁移中:)

回溯如下所示:

line 23, in create_doctors
d = Doctor(user=du)
...
ValueError: Cannot assign "<User: doc01>": "Doctor.user" must be a "User" instance.
第23行,在“创建医生”中
d=医生(用户=du)
...
ValueError:无法分配“”:“Doctor.user”必须是“user”实例。
非常感谢您的支持

编辑: 我找到了解决办法。我只需要给RunPython打电话

RunPython(创建用户,创建医生)

正如所建议的,即使没有将函数移到类外


似乎后续函数必须作为单个RunPython调用的参数进行调用。

我认为问题出在迁移代码中。在迁移类之外定义方法,然后从迁移的RunPython命令调用它

在迁移文件中尝试以下代码。这会奏效的

def create_users(apps, schema_editor):
    u = User.objects.create_superuser('admin', 'admin@aaa.com', 'admin')
    u.save()

    du = User.objects.create_user(username='doc01', password='doc01')
    du.save()

def create_doctors(apps, schema_editor):
    Doctor = apps.get_model('custom_user', 'Doctor')
    du = User.objects.get(username='doc01')
    # We can't import the Doctor model directly, But we can create it. Try this - 
    Doctor.objects.create(user=du)

class Migration(migrations.Migration):

    dependencies = [
        ('custom_user', '0001_initial')
    ]

    operations = [
        migrations.RunPython(create_users, create_doctors),
    ]

运行
migrations.RunPython(创建用户,创建医生)
的建议答案并不能解决您的问题,它只是让您看不见

的第二个参数是将在回滚期间调用的函数,这就是为什么它在向上迁移时不会引发任何异常。您从未调用过函数
create\u documents

您的问题是由
du
不是
用户
实例引起的。这可能是在迁移过程中不使用
应用程序时造成的。获取_model
以获取模型类。您应该改用以下代码:

class Migration(migrations.Migration):
    def create_users(apps, schema_editor):
        User = apps.get_model('auth', 'User')  # Here you get the user programatically, it is a good practise in migrations
        u = User.objects.create_superuser('admin', 'admin@aaa.com', 'admin')
        u.save()

        du = User.objects.create_user(username='doc01', password='doc01')
        du.save()

    def create_doctors(apps, schema_editor):
        Doctor = apps.get_model('custom_user', 'Doctor')
        User = apps.get_model('auth', 'User')  # Here you get the user programatically, it is a good practise in migrations
        du = User.objects.get(username='doc01')

        d = Doctor(user=du)
        d.save()

    dependencies = [
        ('custom_user', '0001_initial')
    ]

    operations = [
        migrations.RunPython(create_users),
        migrations.RunPython(create_doctors),
    ]

嗨,谢谢你的回复,但它不起作用。这个错误和我在帖子里看到的一样。ValueError:无法分配“”:“Doctor.user”必须是“user”实例。刚刚更新了我的答案。试试看。这应该是解决办法。我们不能在迁移中直接导入模型。试试看,让我知道我已经更新了第一篇帖子。谢谢Avinash,问题是我自己对RunPython的调用。现在使用新的RunPython,如果函数在迁移类中,它也可以工作。非常感谢!User=apps.get_model('auth','User')解决了我的错误。对于要引用的每个模型,都必须使用apps.get\u model!