2014-10-20 66 views
33

我有一个数据迁移更新了一些权限。我知道有一些已知的迁移权限问题,我可以通过在迁移中创建权限(而不是使用模型中的元组快捷方式)来避免一些麻烦。在迁移中获取模型ContentType - Django 1.7

迁移:

from __future__ import unicode_literals 
from django.db import migrations, models 
from django.conf import settings 

def create_feature_groups(apps, schema_editor): 
    app = models.get_app('myauth') 

    Group = apps.get_model("auth", "Group") 
    pro = Group.objects.create(name='pro') 

    Permission = apps.get_model("auth", "Permission") 
    ContentType = apps.get_model("contenttypes", "ContentType") 
    invitation_contenttype = ContentType.objects.get(name='Invitation') 

    send_invitation = Permission.objects.create(
     codename='send_invitation', 
     name='Can send Invitation', 
     content_type=invitation_contenttype) 

    pro.permissions.add(receive_invitation)  

class Migration(migrations.Migration): 

    dependencies = [ 
     ('myauth', '0002_initial_data'), 
    ] 

    operations = [ 
      migrations.RunPython(create_feature_groups), 
    ] 

一些试验和错误后,我能够用manage.py migrate,使这项工作,但我发现在测试manage.py test错误。

__fake__.DoesNotExist: ContentType matching query does not exist. 

调试了一下发现,在试运行时(不知道为什么)没有ContentType在迁移至此。按照post的建议,我尝试在自己的迁移中手动更新内容类型。补充:

from django.contrib.contenttypes.management import update_contenttypes 
update_contenttypes(app, models.get_models()) 

取为Invitation模型的内容类型之前。得到了以下错误

File "C:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\contrib\contenttypes\management.py", line 14, in update_contenttypes 
    if not app_config.models_module: 
AttributeError: 'module' object has no attribute 'models_module' 

必须有一些方式可测试的方式来创建数据迁移/更新的权限。

谢谢。

编辑

最后使它加入

from django.contrib.contenttypes.management import update_all_contenttypes 
update_all_contenttypes() 

奇怪的是这一次是不够的

update_contenttypes(apps.app_configs['contenttypes']) 

我很想知道为什么这一切的是需要

工作
+0

对于那些Django的1.8谁想要update_all_contenttypes,请参考这个问题:http://stackoverflow.com/questions/29550102/importerror-cannot -import-name-update-all-contenttypes – jstaab 2015-09-09 23:31:01

回答

6

有一个simila在编写跨越多个应用程序的数据迁移时遇到问题。原来,Django只会将这些模型加载到应用程序注册表中,受迁移状态的“依赖关系”成员影响:https://code.djangoproject.com/ticket/24303

必须基本上向我使用的迁移依赖项添加条目,例如当前正在迁移的应用程序的ForeignKey。

+0

我不明白。你如何确定哪些(不相关的)迁移需要添加为模型的依赖关系? – haki 2015-02-15 11:46:29

+0

基本上,我添加了所有我想用作数据迁移一部分的模型的应用程序。 – Sakuraba 2015-03-13 09:30:35

+0

除了运行update_all_contenttypes(感谢提示),您应该包含('contenttypes','0001_initial')作为迁移依赖项。 – chk 2015-05-11 09:09:31

1
update_contenttypes(apps.app_configs['contenttypes']) 

将更新contenttypes应用的内容类型。

我相信你会想这样做......

update_contenttypes(apps.app_configs['app_label']) 

其中app_label是应用程序,其中邀请模型lives.This将更新您的单一的应用程序的内容类型,所以这将是可用的应用程序标签按照您的原始代码进行查询。

3

因为,我最终花了3-4个小时在这个上面,我正在添加我的解决方案。

问题是当我将多个迁移一起运行时,ContentType和Permission对象未创建。由于我在下一次迁移时引用了这些内容类型和迁移,这就造成了问题。)

但是,如果我使用迁移编号逐个运行它们,它们将正常工作。 (在未来的迁移中被引用)

为了解决这个问题,我添加了一个额外的迁移来创建ContentType和Permission对象。

- - 编码:UTF-8 - -

# Generated by Django 1.10.6 on 2017-03-11 05:59 
from __future__ import unicode_literals 

from django.conf import settings 
from django.db import migrations 


def update_all_contenttypes(**kwargs): 
    from django.apps import apps 
    from django.contrib.contenttypes.management import update_contenttypes 

    for app_config in apps.get_app_configs(): 
     update_contenttypes(app_config, **kwargs) 


def create_all_permissions(**kwargs): 
    from django.contrib.auth.management import create_permissions 
    from django.apps import apps 

    for app_config in apps.get_app_configs(): 
     create_permissions(app_config, **kwargs) 


def forward(apps, schema_editor): 
    update_all_contenttypes() 
    create_all_permissions() 


def backward(apps, schema_editor): 
    pass 


class Migration(migrations.Migration): 
    dependencies = [ 
     migrations.swappable_dependency(settings.AUTH_USER_MODEL), 
     ('contenttypes', '0002_remove_content_type_name'), 
     ('MY_APP', '0123_LAST_MIGRATION'), 
    ] 

    operations = [ 
     migrations.RunPython(forward, backward) 
    ] 
+2

使用Django 1.11 update_contenttypes不再存在,请改用create_contenttypes – oden 2017-05-15 03:25:54