因此,在我花了大部分时间试图绕过南方的数据和模式迁移之后,我觉得我已经接近了 - 但是我的数据迁移转发功能遇到了一些麻烦。 / p>
供参考,这是我原来的模型:
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)
...
我已按照convert_to_south我的应用程序的步骤,以及按照教程#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,南0.7和MySql 5.0.51a。
编辑:删除了Try / Except位,我收到的错误消息是:“ValueError:无法分配”158L“:”General_Note.lead_contact“必须是”Lead_Contact“实例。
不应该将General_Note.lead_contact与Doctor.id绑定为合适的Lead_Contact实例吗?
答案 0 :(得分:1)
尝试将doctor.id
更改为doctor
:
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)
要:
orm.General_Note.objects.create(lead_contact=doctor,
user = "AU",
general_date = "2011-03-12",
general_time = "09:00:00",
general_message = doctor.general_notes)