from django.db import migrations def migrate_legacy_fields(apps, schema_editor): Content = apps.get_model('igny8_core', 'Content') Task = apps.get_model('igny8_core', 'Task') # Migrate legacy fields in Content model for content in Content.objects.all(): if content.categories: # Convert JSON categories to ContentTaxonomy categories = content.categories for category in categories: taxonomy, created = ContentTaxonomy.objects.get_or_create( name=category['name'], slug=category['slug'], taxonomy_type='category' ) content.taxonomies.add(taxonomy) if content.tags: # Convert JSON tags to ContentTaxonomy tags = content.tags for tag in tags: taxonomy, created = ContentTaxonomy.objects.get_or_create( name=tag['name'], slug=tag['slug'], taxonomy_type='tag' ) content.taxonomies.add(taxonomy) content.save() # Migrate legacy fields in Task model for task in Task.objects.all(): task.entity_type = task.content.entity_type task.cluster_role = task.content.cluster_role task.cluster_id = task.content.cluster_id task.save() class Migration(migrations.Migration): dependencies = [ ('igny8_core', '0005_phase3_mark_deprecated_fields'), ] operations = [ migrations.RunPython(migrate_legacy_fields), ]