2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-12-16 09:18:10 +00:00

Apply data migration for templates

This commit is contained in:
Oliver Walters
2025-10-28 11:17:58 +00:00
parent 8bc131c93d
commit 44b9398c5d

View File

@@ -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
)
]