2
我從django 1.8遷移到django 1.9。django 1.9 models_module在遷移應用程序中丟失
我有一個遷移,它向該組添加了一個組user
,然後是一個權限django_comments.add_comment
。與Django的1.8工作遷移看起來像這樣
from django.contrib.contenttypes.management import update_contenttypes from django.contrib.auth.management import create_permissions def create_perms(apps, schema_editor): update_contenttypes(apps.get_app_config('django_comments')) create_permissions(apps.get_app_config('django_comments')) Group = apps.get_model('auth', 'Group') group = Group(name='user') group.save() commentct = ContentType.objects.get_for_model(apps.get_model('django_comments', 'comment')) group.permissions.add([Permission.objects.get(codename='add_comment', content_type_id=commentct)]) group.save() class Migration(migrations.Migration): dependencies = [ ('contenttypes', '0002_remove_content_type_name'), ('django_comments', '0002_update_user_email_field_length') ] operations = [ migrations.RunPython(create_perms, remove_perms) ]
升級到Django的1.9,這是因爲contentType中無法找到引發錯誤。這是因爲update_contenttypes
調用沒有創建必要的content_types。有這條線是函數(django's source code reference)
def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs): if not app_config.models_module: return ...
這app_config.models_module
是在Django 1.9 None
內,但不None
在Django 1.8
如果我替換此代碼
def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs): if not app_config.models_module: #return pass ...
然後一切正常。
事情是我不想改變django的核心代碼。我如何在django 1.9中完成這項工作?
使用Django 1.11 update_contenttypes不再存在,請改用create_contenttypes – oden