2012-11-28 38 views
6

我試圖從一個Django應用程序遷移到另一個基於這個問題的幾個模型How do I migrate a model out of one django app and into a new one?我已經得到了很多工作,但創建第一個遷移時,我得到這個錯誤:contenttypes.contenttype在此遷移中不可用

"The model 'contenttype' from the app 'contenttypes' is not available in this migration." 

谷歌和SO似乎並沒有找到這種情況發生的任何情況下,與上述問題並沒有任何具體的說一下,要麼,除了在代碼中的註釋:

if not db.dry_run: 
    # For permissions to work properly after migrating 
    orm['contenttypes.contenttype'].objects.filter(app_label='common', model='cat').update(app_label='specific') 

真的會升值嗎吃了什麼我做錯了什麼洞察力。

這裏有兩個遷移文件:

創建:

# encoding: utf-8 
import datetime 
from south.db import db 
from south.v2 import SchemaMigration 
from django.db import models 

class Migration(SchemaMigration): 

    def forwards(self, orm): 
     db.rename_table('cars_country', 'general_country') 
     if not db.dry_run: 
      # For permissions to work properly after migrating 
      orm['contenttypes.ContentType'].objects.filter(app_label='cars', model='country').update(app_label='general') 

    def backwards(self, orm): 
     pass 

刪除:

# encoding: utf-8 
import datetime 
from south.db import db 
from south.v2 import SchemaMigration 
from django.db import models 

class Migration(SchemaMigration): 

    depends_on = (
     ('general', '0002_create_country'), 
    ) 

    def forwards(self, orm): 

     db.alter_column('cars_club', 'country_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['general.Country'], null=True)) 


    def backwards(self, orm): 

     db.rename_table('general_country', 'cars_country') 
     db.alter_column('cars_club', 'country_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['cars.Country'], null=True)) 
+0

根據南文檔: _注意,您只能訪問已凍結的模型; South自動包括任何可以通過外鍵或多對多關係到達的內容,但是如果您想添加其他模型,只需將--freeze appname傳遞給./manage.py datamigration命令._ – dgel

+0

因此,它的聲音就像您需要將ContentTypes應用程序在某個時間點傳遞給--freeze一樣。不完全瞭解如何,但可能是正確方向的暗示。 – dgel

+0

假設ContentTypes應用程序不會成爲任何模式遷移的目標,那麼從Django項目中導入模型的難度有多大? _from django.contrib.contenttypes.models import ContentType_ _ContentType.objects.filter(app_label ='common',model ='cat')。update(app_label ='specific')_ – JCJS

回答

4

好了,找到了解決辦法。 dgel的凍結通知讓我檢查了南方文檔,並且發現了有關ORM遷移的通知:這是通過將模型序列化爲每個遷移底部的稱爲模型的大字典來完成的。很容易看到;它是底部密集代碼的大塊。

所以基本上我只需要將orm ['contenttypes.contenttype]移動到第二個遷移文件,因爲contenttype模型字典已經在那裏了。現在一切似乎都應該如此。

+2

或者簡單地將您的數據遷移創建爲: ' ./manage.py datamigration app_name --auto --freeze contenttypes' – geoom