2012-09-26 39 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查詢來加快速度。