南迁和多对多地区的变化 [英] South migrations and changes to many-to-may fields

查看:145
本文介绍了南迁和多对多地区的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,用于向客户发送通讯。我有一个模型定义如下

  from django.auth.models import User 

class Newsletter
owner = models.ForeignKey(User,related_name ='+',blank = False)
sent = models.BooleanField(default = False)
date_created = models.DateTimeField (auto_now_add = True)
date_sent = models.DateTimeField(null = True)
subject = models.CharField(max_length = 255)
content = HTMLField()
recipient = models.ManyToManyField(User,related_name ='+')

稍后,我发现我可能需要将这些发送给没有用户帐户的人,所以我定义了一个电子邮件模型



  class Email(models.Model): 
email = models.CharField(max_length = 255)

并将收件人字段更改为

  recipients = models.ManyToManyField(Email,related_name ='+')
/ pre>

之后,我运行了schemamigration命令,但South声称没有更改。我已经做了几次手动操作表和索引的尝试,但在某种程度上,由于它是一个新的应用程序,我可以删除所有现有的表,删除所有的数据并从头重新创建初始迁移。这是一个问题,如果我真的需要保留数据,我该怎么做这样的迁移。

解决方案

如果你需要保存数据,您将需要将其拆分为3个迁移(模式,数据,模式),如下所示:

  class Migration模式迁移)

def转发(self,orm):
db.add_column(
u'yourapp_newsletter_recipients',
'email_id',
self。 gf(
'django.db.models.fields.related.ForeignKey'
)(to = orm.Email)

请注意,您应该在迁移模型属性中使用旧模式。



然后,您的数据迁移将填充电子邮件字段。然后再架构迁移:

  class Migration(SchemaMigration):

def forward(self,orm) :
db.delete_column(u'yourapp_newsletter_recipients','user_id')

在这一个您应该已经在迁移模型中的收件人字段中具有正确的m2m字段['newslatter']


I was working on an app that is supposed to send newsletters to customers. I had a model defined like this

from django.auth.models import User

class Newsletter(models.Model):                                                                  
    owner = models.ForeignKey(User, related_name='+', blank=False)                                                                                                                                                  
    sent = models.BooleanField(default=False)                                                                                                                                                                       
    date_created = models.DateTimeField(auto_now_add=True)                                                                                                                                                          
    date_sent = models.DateTimeField(null=True)                                                                                                                                                                     
    subject = models.CharField(max_length=255)                                                                                                                                                                      
    content = HTMLField()                                                                                                                                                                                           
    recipients = models.ManyToManyField(User, related_name='+')                                                                                                                                                    

Later on, I found out that I might need to send these to people who do not have user accounts, so I defined an email model

class Email(models.Model):                                                                                                                                                                                          
    email = models.CharField(max_length=255)                                                          

and changed the recipients field to read

recipients = models.ManyToManyField(Email, related_name='+')

After this, I ran schemamigration command, but South claimed that there are no changes. I have made several attempts to manually manipulate tables and indexes, but at some point figured that since it is a new app, I can just drop all the existing tables, remove all igrations and recreate initial migration from scratch. This poses a question though, how do I do a migration like this if I really need to preserve the data.

解决方案

If you need to preserve data you will need to split it into 3 migrations (schema, data, schema) like this:

class Migration(SchemaMigration):

    def forwards(self, orm):
        db.add_column(
            u'yourapp_newsletter_recipients',
            'email_id',
            self.gf(
                'django.db.models.fields.related.ForeignKey'
            )(to=orm.Email)
        )

Notice that you should have your old schema in the migration's models property.

Then your data migration that fills the email field. Then schema migration again:

class Migration(SchemaMigration):

    def forwards(self, orm):
        db.delete_column(u'yourapp_newsletter_recipients', 'user_id')

In this one you should already have correct m2m field for recipients field in the migration's models['newslatter']

这篇关于南迁和多对多地区的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆