# Generated by Django 2.2.16 on 2020-09-04 12:06 from django.db import migrations from logging import getLogger log = getLogger(__name__) # Oprava migrací 0065 a 0066, kde jsem špatně pochopil django-polymorphic # Pomocná funkce -- děláme to samé pro obě polymorfní hierarchie def fix_ctypes(parent: str, children, apps, schema_editor): Parent = apps.get_model('seminar', parent) ContentType = apps.get_model('contenttypes', 'ContentType') # Nejdřív všechno smažeme: Parent.objects.update(polymorphic_ctype=None) # Opravíme děti for clsname in children: Model = apps.get_model('seminar', clsname) ct = ContentType.objects.get_for_model(Model) Model.objects.update(polymorphic_ctype=ct) # Ostatní instance mají mít explicitně content type pro rodiče new_ct = ContentType.objects.get_for_model(Parent) for obj in Parent.objects.filter(polymorphic_ctype__isnull=True): # Po úpravě pořadí migrací se tohle stane pro každý problém. Není to správně, ale warning moc otravuje… log.info(f"{parent} \"{obj}\" neměl content type -- nejspíš to je instance přímo {parent}!") obj.polymorphic_ctype=new_ct obj.save() def fix_treenode(apps, schema_editor): children = ['RocnikNode', 'CisloNode', 'MezicisloNode', 'TemaVCisleNode', 'OrgTextNode', 'UlohaZadaniNode', 'UlohaVzorakNode', 'PohadkaNode', 'TextNode', 'CastNode', 'ReseniNode'] fix_ctypes("TreeNode", children, apps, schema_editor) def fix_problem(apps, schema_editor): children = ['Tema', 'Clanek', 'Uloha'] # FIXME: Konfera z nějakého důvodu tenhle field vůbec nemá, asi je to špatně. fix_ctypes("Problem", children, apps, schema_editor) class Migration(migrations.Migration): dependencies = [ ('seminar', '0077_auto_20200318_2146'), ] operations = [ migrations.RunPython(fix_treenode, migrations.RunPython.noop), migrations.RunPython(fix_problem, migrations.RunPython.noop), ]