Próbuję zaimplementować datamigration przy użyciu natywnego systemu migracji django 1.7. Oto, co zrobiłem.Django 1.7 datamigracja i grupy użytkowników
# -*- 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),
]
Ale gdy próbuję uruchomić migrację, mam wyjątek LookupError mówi mi, że żadna aplikacja z etykietą „auth” można znaleźć.
Jak mogę utworzyć moje grupy w czysty sposób, który może być również użyty w testach jednostkowych?
Spróbuj 'app.get_registered_model' i/lub zależeć od' ("auth", "group") ". To jest rodzaj losowej porady, ponieważ sam jestem w trakcie rozumienia rejestru. Pomogło mi to rozwiązać podobny problem. –
W django 1.8 mananagers obiektów mogą być dostępne podczas migracji. W szczególności teraz Twój kod powinien działać tak jak jest –