2016-02-11 33 views
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中完成这项工作?

回答

3

好的,感谢#django IRC(用户knbk)的一些帮助,我发现了一个丑陋的解决方法,但至少它的工作原理!

更改此两行

 
    update_contenttypes(apps.get_app_config('django_comments')) 
    create_permissions(apps.get_app_config('django_comments')) 

写这篇文章,而不是

 
    app = apps.get_app_config('django_comments') 
    app.models_module = app.models_module or True 
    update_contenttypes(app) 
    create_permissions(app) 

现在它工作得很好。

+0

使用Django 1.11 update_contenttypes不再存在,请改用create_contenttypes – oden

相关问题