数据迁移期间的Django south ValueError

数据迁移期间的Django south ValueError,django,foreign-keys,django-south,data-migration,Django,Foreign Keys,Django South,Data Migration,我正在对使用Django构建的系统进行一些更新,现在我遇到了一些南方数据迁移的问题 class Migration(DataMigration): def forwards(self, orm): for cargo in orm['cargo.Cargo'].objects.all(): profile = orm['accounts.UserProfile'].objects.get(user=cargo.customer) cargo.compan

我正在对使用Django构建的系统进行一些更新,现在我遇到了一些南方数据迁移的问题

class Migration(DataMigration):

def forwards(self, orm):
    for cargo in orm['cargo.Cargo'].objects.all():
        profile = orm['accounts.UserProfile'].objects.get(user=cargo.customer)
        cargo.company = profile.company
        cargo.save()
我有一个模型Cargo,它有一个auth.User的外键,现在我想向另一个与auth.User相关的模型(公司)添加一个外键

class Cargo(models.Model):
    company = models.ForeignKey(
        'accounts.Company',
        related_name='cargo_company',
        verbose_name='empresa',
        null=True,
        blank=True
    )

    customer = models.ForeignKey(
        'auth.User',
        related_name='cargo_customer',
        verbose_name='embarcador',
        limit_choices_to={'groups__name': 'customer'},
        null=True,
        blank=True
    )
我还有一个UserProfile模型,它与auth.User和Company有关,如下所示:

class UserProfile(models.Model):
    company = models.ForeignKey(
        Company, 
        verbose_name='Empresa', 
        null=True
    )
    user = models.OneToOneField('auth.User')
我创建并运行了一个schemamigration,将company字段添加到Cargo,然后创建了一个datamigration,这样我就可以填充我所有货物的company字段。我想到的是:

class Migration(DataMigration):

def forwards(self, orm):
    try:
        from cargobr.apps.accounts.models import UserProfile
    except ImportError:
        return

    for cargo in orm['cargo.Cargo'].objects.all():
        profile = UserProfile.objects.get(user=cargo.customer)
        cargo.company = profile.company
        cargo.save()
但当我尝试运行它时,会出现以下错误:

ValueError: Cannot assign "<Company: Thiago Rodrigues>": "Cargo.company" must be a "Company" instance.
ValueError:无法分配“”:“Cargo.company”必须是“company”实例。
但正如你在上面的模型中看到的,这两个字段是同一类型的。。。谁能用这个给我点灯吗?我在Django 1.3.1和South 0.7.3上


编辑:如下所述,
用户档案
公司
模型位于
账户
模块中,
货物
位于
货物
模块中。因此,简而言之,我有
帐户.UserProfile
帐户.Company
cargo.cargo

您使用的型号版本可能不匹配,因为您直接导入了:

from cargobr.apps.accounts.models import UserProfile
相反,尝试在迁移中使用
orm
引用该模型

class Migration(DataMigration):

def forwards(self, orm):
    for cargo in orm['cargo.Cargo'].objects.all():
        profile = orm['accounts.UserProfile'].objects.get(user=cargo.customer)
        cargo.company = profile.company
        cargo.save()

UserProfile
在哪个模块中?似乎您直接从那里引用了
公司
,但在
货物
中,您使用了
“accounts.Company”
。你有两个同名的类吗?
UserProfile
Company
都在
accounts
应用程序中,而
Cargo
Cargo
应用程序中,因此
s为什么我在
UserProfile
中直接引用
公司
,而不是在
Cargo
中,这可能是由于您直接导入了
UserProfile
。根据,从不同应用访问模型的一种方法是使用
orm['someapp.SomeModel']
,因此在您的情况下,您可以使用
orm['accounts.UserProfile']
而不是从cargobr…
访问模型。此机制确保模型的实例与创建数据迁移时的实例相同。谢谢!为了解决这个问题,我只需要做一件额外的事情:我必须重新创建数据迁移,这次使用
--freeze accounts
参数,以便在迁移中包括这个模型。然后它完美地工作了!干杯,已经用了很长时间了,但就在最近,我开始问问题。。希望将来我能帮助别人