# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations


def fix_employment_groups(apps, schema_editor):
    # We can't import the Employment or Group model directly as it may be a
    # newer version than this migration expects. We use the historical version.
    Group = apps.get_model("auth", "Group")
    Employment = apps.get_model("rsr", "Employment")
    for employment in Employment.objects.filter(group=None):
        employment.group = Group.objects.get(name='Users')
        employment.save()


class Migration(migrations.Migration):

    dependencies = [
        ('rsr', '0086_auto_20160921_0947'),
    ]

    operations = [
        migrations.RunPython(fix_employment_groups),
    ]

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations


def fix_employment_groups(apps, schema_editor):
    # We can't import the Employment or Group model directly as it may be a
    # newer version than this migration expects. We use the historical version.
    Group = apps.get_model("auth", "Group")
    Employment = apps.get_model("rsr", "Employment")
    for employment in Employment.objects.filter(group=None):
        try:
            employment.group = Group.objects.get(name='Users')
            employment.save()
        except Exception as e:
            print(e)


class Migration(migrations.Migration):

    dependencies = [
        ('rsr', '0086_auto_20160921_0947'),
    ]

    operations = [
        migrations.RunPython(fix_employment_groups),
    ]

Fix broken migration with try-except blocks
Fix broken migration with try-except blocks

Duplicate key errors were being caused if an employment similar to the
one being created by the migration already existed.
