2013年6月28日 星期五

Django syncdb的加強,更新異動後的model欄位

Django中常常會使用manage.py syncdb,將model中建立的class轉換為db中的table,
但若是之後class的欄位有改動,syncdb無法輕易的alter table schema,
South這套程式可以處理這個問題  http://south.aeracode.org/
簡單的操作如下(假設已安裝South完成,安裝說明參考http://south.readthedocs.org/en/latest/installation.html)

第一次同步資料使用
manage.py schemamigration article--initial

South會自動建立一個migrations的目錄





第二次後皆使用
manage.py schemamigration article--auto

目錄階層如下

schemamigration 其功能是比較程式中model欄位版本的差異,真正要將model --> table需要使用以下指令
manage.py migrate article
DB table中就會看到修改後的資料


若是開發的App一開始沒有使用South,DB已經建立了table,這情況可以先使用
manage.py convert_to_south article
之後再使用上述的步驟即可

沒有留言:

張貼留言