写“转发”时遇到问题南方数据迁移

发布于 2024-10-21 16:15:49 字数 1727 浏览 2 评论 0原文

因此,在花了一天的大部分时间试图围绕南方的数据和模式迁移进行思考之后,我觉得我已经很接近了——但我的数据迁移转发功能遇到了一些问题。

作为参考,这是我的原始模型:

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 实例?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2024-10-28 16:15:49

尝试将 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文