# Generated by Django 3.1.3 on 2022-02-18 08:17 from django.db import migrations, models, transaction import django.db.models.deletion from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_AFTER_STATE_BIOTOPES_ID def migrate_entries_974_to_654(apps, schema_editor): CompensationState = apps.get_model("compensation", "CompensationState") KonovaCode = apps.get_model("codelist", "KonovaCode") all_states = CompensationState.objects.all() with transaction.atomic(): for state in all_states: code_from_654 = KonovaCode.objects.get( short_name=state.biotope_type.short_name, code_lists__in=[CODELIST_BIOTOPES_ID], is_archived=False, is_leaf=True, ) state.biotope_type = code_from_654 state.save() old_list_states = CompensationState.objects.filter( biotope_type__code_lists__in=[CODELIST_AFTER_STATE_BIOTOPES_ID] ) if old_list_states.count() > 0: raise Exception("Still unmigrated values!") class Migration(migrations.Migration): dependencies = [ ('codelist', '0001_initial'), ('compensation', '0004_auto_20220210_1402'), ] operations = [ migrations.RunPython(migrate_entries_974_to_654), migrations.AlterField( model_name='compensationstate', name='biotope_type', field=models.ForeignKey(blank=True, limit_choices_to={'code_lists__in': [654], 'is_archived': False, 'is_selectable': True}, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='codelist.konovacode'), ), ]