2012-09-26 38 views
1

我目前有类似这样的设置。Django South - 将CharField更改为ForeignKey

#models.py 
class Donate(model.Model): 
      item_locations = models.CharField(max_length=150) 
#forms.py 
    ItemChoices = (('item1','item1'),('item2','item2'),('item3','item3')) 
    class DonateForm(ModelForm): 
     item_locations = CharField(choices=ItemChoices) 
     class Meta: 
       model = Donate 

我想改变item_locations到一个ForeignKey,让用户能够改变自己的项目列表。我已经做了另一类称为ItemLocations并将其更改为

item_locations = models.ForeignKey(ItemLocation) 

如何让这个数据库可以保持以前的纪录? Donor1具有“Item1”的项目位置,它们在将其更改为外键(其中查找将是“item_location_id”)后如何保留该数据?

另外,这是否像南迁移一样简单?我的数据库是MySql。

./manage.py schemamigration donate --auto 

回答

3

简单的方法是:

  1. 添加新的外键列。
  2. 使用get_or_create迁移数据。
  3. 删除旧列。

您可以通过预先计算事物并使用update查询来加快速度。