diff --git a/src/backend/InvenTree/common/migrations/0041_auto_20251028_1112.py b/src/backend/InvenTree/common/migrations/0041_auto_20251028_1112.py new file mode 100644 index 0000000000..204fb9afe7 --- /dev/null +++ b/src/backend/InvenTree/common/migrations/0041_auto_20251028_1112.py @@ -0,0 +1,54 @@ +# Generated by Django 4.2.25 on 2025-10-28 11:12 + +from django.db import migrations + + +def copy_templates(old_model, new_model): + """Copy from one model type to another.""" + + templates = [] + + for template in old_model.objects.all(): + templates.append(new_model( + name=template.name, + description=template.description, + units=template.units, + checkbox=template.checkbox, + choices=template.choices, + selectionlist=template.selectionlist, + )) + + + if len(templates) > 0: + new_model.objects.bulk_create(templates) + print(f"Migrated {len(templates)} ParameterTemplate instances.") + + +def forward_copy_templates(apps, schema_editor): + """Forward migration: copy from PartParameterTemplate to ParameterTemplate.""" + PartParameterTemplate = apps.get_model("part", "PartParameterTemplate") + ParameterTemplate = apps.get_model("common", "ParameterTemplate") + + copy_templates(PartParameterTemplate, ParameterTemplate) + + +def reverse_copy_templates(apps, schema_editor): + """Reverse migration: copy from ParameterTemplate to PartParameterTemplate.""" + ParameterTemplate = apps.get_model("common", "ParameterTemplate") + PartParameterTemplate = apps.get_model("part", "PartParameterTemplate") + + copy_templates(ParameterTemplate, PartParameterTemplate) + + +class Migration(migrations.Migration): + + dependencies = [ + ("common", "0040_parametertemplate"), + ] + + operations = [ + migrations.RunPython( + forward_copy_templates, + reverse_code=reverse_copy_templates + ) + ]