Django迁移无法将用户实例分配给用户字段

Posted

技术标签:

【中文标题】Django迁移无法将用户实例分配给用户字段【英文标题】:Django migration cannot assign User instance to user field 【发布时间】:2020-02-01 19:04:23 【问题描述】:

问题


最近,我为SubscriptionAccount 模型创建了一个新迁移,它用User 模型的链接替换了email 字段。在此迁移中,我还进行了一次小型数据迁移,它使用电子邮件获取用户并将其设置为SubscriptionAccount.user。但是,由于某种原因,迁移不想将用户分配给新创建的user 字段。抱怨它不能分配一个 User 实例蚂蚁,它期望一个 User 实例。这很奇怪,因为这正是我正在做的事情。

错误


出现以下错误:
Traceback (most recent call last):
  File "./manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/project/hypernodedb/migrations/0069_link_subscription_account_to_user.py", line 26, in migrate_email_to_user
    subscription_account.user = user
  File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 220, in __set__
    self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "<User: admin@example.com>": "SubscriptionAccount.user" must be a "User" instance.

我尝试了什么


我尝试将我获取的用户转换为实际的User 实例,但无济于事。我做了很多调试,发现模块有一些不同,看到在某些时候用户的模块会更改为__fake__。然而,这只是在 Django 内部,所以我不确定那是什么。我检查了迁移中的所有类型,除了__fake__ 模块外,一切似乎都井井有条。

我也尝试升级到最新的 django 和 DRF 版本,但没有成功。

代码


def migrate_email_to_user(apps, schema_editor):
    SubscriptionAccount = apps.get_model('hypernodedb', 'SubscriptionAccount')
    User = get_user_model()

    for subscription_account in SubscriptionAccount.objects.all():
        user, _ = User.objects.get_or_create(username=subscription_account.email)
        subscription_account.user = user
        subscription_account.save()


def revert_migrate_email_to_user(apps, schema_editor):
    SubscriptionAccount = apps.get_model('hypernodedb', 'SubscriptionAccount')

    for subscription_account in SubscriptionAccount.objects.all():
        subscription_account.email = subscription_account.user.username
        subscription_account.save()


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('hypernodedb', '0068_change_app_name_validation_error'),
    ]

    operations = [
        migrations.AddField(
            model_name='subscriptionaccount',
            name='user',
            field=models.OneToOneField(null=True, default=None, on_delete=django.db.models.deletion.CASCADE,
                                       to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
        migrations.RunPython(
            code=migrate_email_to_user,
            reverse_code=revert_migrate_email_to_user,
        ),
        migrations.RemoveField(
            model_name='subscriptionaccount',
            name='email',
        ),
    ]

以下是迁移时的SubscriptionAccount

class SubscriptionAccount(TimeStampedModel):
    user = models.OneToOneField(get_user_model(), null=True)
    external_id = models.CharField(max_length=255)

    def __str__(self):
        return ' - '.format(self.user.username, self.external_id)

这是更改前的模型:

class SubscriptionAccount(TimeStampedModel):
    # TODO: link this to a user model in our auth system
    email = models.CharField(max_length=255, unique=True)
    external_id = models.CharField(max_length=255)

    def __str__(self):
        return ' - '.format(self.user.username, self.external_id)

【问题讨论】:

【参考方案1】:

原来调用get_user_model() 不适合在迁移中使用。如果你仔细想想,这是合乎逻辑的。因为你不应该直接在迁移中导入模型。然而,迁移错误让我失望了,抱怨它期待的是 User 而不是 User。此外,使用 get_user_model() 函数与直接导入函数有点不同(即,如果您使用不同的 User 模型会怎样)。

解决这个问题的方法是从apps导入User模型:

User = apps.get_model('auth', 'User')

此后迁移顺利。

【讨论】:

以上是关于Django迁移无法将用户实例分配给用户字段的主要内容,如果未能解决你的问题,请参考以下文章

将密码字段迁移到 Django

从 django 用户模型迁移到自定义用户模型

无法在视图中设置 Django 表单的字段值

从 Django 1.11 迁移到 2.1:“fieldsets[2][1]”中有重复的字段

如何将自定义用户模型迁移到 Django 中的不同应用程序?

将用户帐户从Joomla迁移到Django