我正在嘗試使用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