因此,在我度过了一天中大部分时间试图在 South 进行数据和模式迁移之后,我觉得我已经接近了 - 但我的数据迁移转发功能遇到了一些问题。
作为参考,这是我的原始模型:
class Lead_Contact(models.Model):
...
general_notes = models.TextField(blank=True)
...
我正在尝试迁移到以下内容:
class Lead_Contact(models.Model):
...
# General_notes has been removed from here...
...
class General_Note(models.Model):
#...and added as a foreign key here.
...
lead_contact = models.ForeignKey('Lead_Contact', null=True, blank=True)
user = models.CharField(max_length=2, choices=USER_CHOICES)
general_date = models.DateField(blank = True, null=True)
general_time = models.TimeField(blank = True, null=True)
general_message = models.TextField(blank=True)
...
我已按照步骤将我的应用程序转换为南,并按照教程 #3 添加我的表,然后创建我的数据迁移,然后在第二次架构迁移中删除旧的 Lead_contact.general_notes 字段。
问题是编写我实际的 Forwards() 方法;我正在尝试将旧 general_notes 字段中的数据写到 General_Note 表中:
class Migration(DataMigration):
def forwards(self, orm):
for doctor in orm.Lead_Contact.objects.all():
orm.General_Note.objects.create(lead_contact=doctor.id, user = "AU", general_date = "2011-03-12", general_time = "09:00:00", general_message = doctor.general_notes)
def backwards(self, orm):
for note in orm.General_Note.objects.all():
new_gn = orm.Lead_Contact.objects.get(id=note.lead_contact)
new_gn.general_notes = note.general_message
new_gn.save()
作为参考,我使用的是 django 1.2、South 0.7 和 MySql 5.0.51a。
编辑:删除了 Try/Except 位,我收到的错误消息是:“ValueError:无法分配“158L”:“General_Note.lead_contact”必须是“Lead_Contact”实例。
将 General_Note.lead_contact 绑定到 Doctor.id 不应该是合适的 Lead_Contact 实例吗?