我正在尝试使用django 1.7本机迁移系统实现数据迁移。这是我所做的。Django 1.7数据迁移和用户组
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def create_basic_user_group(apps, schema_editor):
"""Forward data migration that create the basic_user group
"""
Group = apps.get_model('auth', 'Group')
Permission = apps.get_model('auth', 'Permission')
group = Group(name='basic_user')
group.save()
perm_codenames = (
'add_stuff',
'...',
)
# we prefere looping over all these in order to be sure to fetch them all
perms = [Permission.objects.get(codename=codename)
for codename in perm_codenames]
group.permissions.add(*perms)
group.save()
def remove_basic_user_group(apps, schema_editor):
"""Backward data migration that remove the basic_user group
"""
group = Group.objects.get(name='basic_user')
group.delete()
class Migration(migrations.Migration):
"""This migrations automatically create the basic_user group.
"""
dependencies = [
]
operations = [
migrations.RunPython(create_basic_user_group, remove_basic_user_group),
]
但是当我尝试运行的迁移,我得到了LookupError例外告诉我,与标签“权威性”没有应用程序可以被发现。
如何在单元测试中以干净的方式创建我的组?
尝试'app.get_registered_model'和/或依赖于'( 'auth','group')'。 这是一种随机的建议,因为我仍然在自己理解注册表的过程。它帮助我解决了类似的问题。 – 2014-10-09 09:38:36
在django 1.8中,对象管理者可以在迁移过程中使用。特别是现在您的代码应该按原样工作 – 2015-05-19 13:47:08