我有一个更新某些权限的数据迁移。我知道迁移中存在一些已知的权限问题,通过在迁移中创建权限(而不是使用模型中的元组快捷方式),我可以避免一些麻烦。
迁徙:
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 (不确定原因)。按照这个帖子中的建议,我尝试在迁移它自己的过程中手动更新内容类型。加:
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'])我很想知道为什么这一切都是必要的
发布于 2019-09-30 14:17:33
对于Django 2.1,我必须从全局注册表导入应用程序,因为传入迁移的应用程序是django.db.migrations.state.AppConfigStub的实例,没有填充的models_module属性。create_contenttypes正在检查这个属性。
from django.apps.registry import Apps, apps as global_apps
from django.contrib.contenttypes.management import create_contenttypes
from django.db import migrations
def add_permision(apps: Apps, schema_editor):
my_app_config = global_apps.get_app_config('my_app')
create_contenttypes(my_app_config)
...发布于 2018-07-16 17:49:47
答案是:
apps.get_model('contenttypes', 'ContentType') )我今天就需要它。
发布于 2017-03-14 16:19:30
因为,我在这个问题上花了3-4个小时--我在增加我的解决方案。
问题是,当我一起运行多个迁移时,没有创建ContentType和权限对象。由于我在下一次迁移中引用了这些内容类型和迁移,这就造成了问题。)
但是,如果我使用迁移号一个接一个地运行它们,它们就能正常工作。(在今后的移徙中提到)
为了解决这个问题,我在中间添加了一个额外的迁移,以创建ContentType和权限对象。
# -*- coding: 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)
]https://stackoverflow.com/questions/26464838
复制相似问题