开发者

Trouble writing a "Forwards" for Data Migration in South

So after spending the better part of my day off trying to wrap my head around data and schema migrations in South, I feel like I'm getting close -- but I'm having some trouble with my datamigration forwards function.

For reference, here was my original model:

class Lead_Contact(models.Model):
...
general_notes = models.TextField(blank=True)
...

I'm attempting to migrate to the following:

class Lead_Contact(models.Model):
...
# General_notes has been removed from here...
...

class General_Note(models.Model):
#...and added as a fore开发者_如何学Pythonign 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)
    ...

I've followed the steps to convert_to_south my app, as well as followed tutorial #3 to add my table, then create my datamigration, and then remove the old Lead_contact.general_notes field in a second schema migration.

The problem is writing my actual Forwards() method; I'm attempting to write out the data from the old general_notes field into the General_Note table:

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()

For reference, I'm using django 1.2, south 0.7, and MySql 5.0.51a.

Edit: Removed the Try/Except bits, and the error message I'm getting is: "ValueError: Cannot assign "158L": "General_Note.lead_contact" must be a "Lead_Contact" instance.

Shouldn't tying General_Note.lead_contact to Doctor.id be an appropriate Lead_Contact instance?


Try changing doctor.id to 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)

To:

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜