From 019494420ad988e5fdbae9aee716afa66337475c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 24 Nov 2025 12:44:56 +0000 Subject: [PATCH] Fixes for table structure --- .../0040_parametertemplate_parameter.py | 5 ++--- .../common/migrations/0041_auto_20251028_1112.py | 15 ++++++++++----- src/backend/InvenTree/common/models.py | 3 ++- src/backend/InvenTree/company/models.py | 8 ++++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/backend/InvenTree/common/migrations/0040_parametertemplate_parameter.py b/src/backend/InvenTree/common/migrations/0040_parametertemplate_parameter.py index e7e255d565..23a18a3ca2 100644 --- a/src/backend/InvenTree/common/migrations/0040_parametertemplate_parameter.py +++ b/src/backend/InvenTree/common/migrations/0040_parametertemplate_parameter.py @@ -2,7 +2,6 @@ import InvenTree.models import InvenTree.validators -import common.validators from django.conf import settings import django.core.validators from django.db import migrations, models @@ -41,9 +40,9 @@ class Migration(migrations.Migration): ( "model_type", models.ForeignKey( - blank=True, + blank=True, null=True, help_text="Target model type for this parameter template", - on_delete=django.db.models.deletion.CASCADE, + on_delete=django.db.models.deletion.SET_NULL, to="contenttypes.contenttype", verbose_name="Model type", ), diff --git a/src/backend/InvenTree/common/migrations/0041_auto_20251028_1112.py b/src/backend/InvenTree/common/migrations/0041_auto_20251028_1112.py index 5407ae0b59..ac497afe76 100644 --- a/src/backend/InvenTree/common/migrations/0041_auto_20251028_1112.py +++ b/src/backend/InvenTree/common/migrations/0041_auto_20251028_1112.py @@ -89,7 +89,7 @@ def copy_part_parameters(apps, schema_editor): checkbox=template.checkbox, choices=template.choices, selectionlist=template.selectionlist, - model_type='' + model_type=None )) if len(templates) > 0: @@ -101,13 +101,15 @@ def copy_part_parameters(apps, schema_editor): # Next, copy PartParameter instances to Parameter instances parameters = [] + content_type = apps.get_model("contenttypes", "ContentType").objects.get(app_label='part', model='part') + for parameter in PartParameter.objects.all(): # Find the corresponding ParameterTemplate template = ParameterTemplate.objects.get(name=parameter.template.name) parameters.append(Parameter( template=template, - model_type='part', + model_type=content_type, model_id=parameter.part.id, data=parameter.data, data_numeric=parameter.data_numeric, @@ -121,7 +123,7 @@ def copy_part_parameters(apps, schema_editor): Parameter.objects.bulk_create(parameters) print(f"\nMigrated {len(parameters)} PartParameter instances.") - assert Parameter.objects.filter(model_type='part').count() == len(parameters) + assert Parameter.objects.filter(model_type=content_type).count() == len(parameters) def copy_manufacturer_part_parameters(apps, schema_editor): @@ -133,6 +135,8 @@ def copy_manufacturer_part_parameters(apps, schema_editor): parameters = [] + content_type = apps.get_model("contenttypes", "ContentType").objects.get(app_label='company', model='manufacturerpart') + for parameter in ManufacturerPartParameter.objects.all(): # Find the corresponding ParameterTemplate template = ParameterTemplate.objects.filter(name=parameter.template.name).first() @@ -143,12 +147,13 @@ def copy_manufacturer_part_parameters(apps, schema_editor): name=parameter.name, description='', units=parameter.units, + model_type=None, checkbox=False ) parameters.append(Parameter( template=template, - model_type='manufacturerpart', + model_type=content_type, model_id=parameter.manufacturer_part.id, data=parameter.value, data_numeric=convert_to_numeric_value(parameter.value), @@ -159,7 +164,7 @@ def copy_manufacturer_part_parameters(apps, schema_editor): Parameter.objects.bulk_create(parameters) print(f"\nMigrated {len(parameters)} ManufacturerPartParameter instances.") - assert Parameter.objects.filter(model_type='manufacturerpart').count() == len(parameters) + assert Parameter.objects.filter(model_type=content_type).count() == len(parameters) class Migration(migrations.Migration): diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index e4459216f6..ee9f2106d9 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -2476,8 +2476,9 @@ class ParameterTemplate( # TODO: Reintroduce validator for model_type model_type = models.ForeignKey( ContentType, - on_delete=models.CASCADE, + on_delete=models.SET_NULL, blank=True, + null=True, verbose_name=_('Model type'), help_text=_('Target model type for this parameter template'), ) diff --git a/src/backend/InvenTree/company/models.py b/src/backend/InvenTree/company/models.py index f642e244d2..3b1a94470b 100644 --- a/src/backend/InvenTree/company/models.py +++ b/src/backend/InvenTree/company/models.py @@ -298,7 +298,7 @@ class Contact(InvenTree.models.InvenTreeMetadataModel): @staticmethod def get_api_url(): - """Return the API URL associated with the Contcat model.""" + """Return the API URL associated with the Contact model.""" return reverse('api-contact-list') company = models.ForeignKey( @@ -383,7 +383,7 @@ class Address(InvenTree.models.InvenTreeModel): @staticmethod def get_api_url(): - """Return the API URL associated with the Contcat model.""" + """Return the API URL associated with the Contact model.""" return reverse('api-address-list') company = models.ForeignKey( @@ -586,7 +586,7 @@ class ManufacturerPart( class ManufacturerPartParameter(InvenTree.models.InvenTreeModel): - """A ManufacturerPartParameter represents a key:value parameter for a MnaufacturerPart. + """A ManufacturerPartParameter represents a key:value parameter for a ManufacturerPart. This is used to represent parameters / properties for a particular manufacturer part. @@ -891,7 +891,7 @@ class SupplierPart( ) def base_quantity(self, quantity=1) -> Decimal: - """Calculate the base unit quantiy for a given quantity.""" + """Calculate the base unit quantity for a given quantity.""" q = Decimal(quantity) * Decimal(self.pack_quantity_native) q = round(q, 10).normalize()