From 7117c333790966878008133afe61c3ccff4464ed Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 10 Aug 2021 10:58:11 +1000 Subject: [PATCH] Raise validation error if the manufacturer part does not point to the correct part --- InvenTree/company/models.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 3b731381b8..b097de8cf3 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -9,6 +9,8 @@ import os from django.utils.translation import ugettext_lazy as _ from django.core.validators import MinValueValidator +from django.core.exceptions import ValidationError + from django.db import models from django.db.models import Sum, Q, UniqueConstraint @@ -479,6 +481,18 @@ class SupplierPart(models.Model): # This model was moved from the 'Part' app db_table = 'part_supplierpart' + def clean(self): + + super().clean() + + # Ensure that the linked manufacturer_part points to the same part! + if self.manufacturer_part and self.part: + + if not self.manufacturer_part.part == self.part: + raise ValidationError({ + 'manufacturer_part': _("Linked manufacturer part must reference the same base part"), + }) + part = models.ForeignKey('part.Part', on_delete=models.CASCADE, related_name='supplier_parts', verbose_name=_('Base Part'),