我正在从django 1.8迁移到django 1.9 .
我有一个迁移,它添加了一个组 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
,但在django 1.8中是 not None
如果我替换此代码
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中完成这项工作?
1 回答
好的,感谢#django IRC(用户knbk)的一些帮助,我找到了一个丑陋的解决方法,但至少它有效!
改变这两行
写下这个
现在它工作得很好 .