From 2aef05c1c358c8252f3282a7b81a89f4f2ad3815 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 6 Dec 2023 09:48:49 +1100 Subject: [PATCH] Fix validation for Build model (#6038) * Fix validation for Build model - Move checks into clean() method - Allows errors to be handled cleanly when editing from the admin interface * Fix unit test --- InvenTree/build/models.py | 17 +++++++++++------ InvenTree/build/test_build.py | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 737f60f7fa..7c6b9c6bac 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -109,12 +109,6 @@ class Build(MPTTModel, InvenTree.mixins.DiffMixin, InvenTree.models.InvenTreeBar self.validate_reference_field(self.reference) self.reference_int = self.rebuild_reference_field(self.reference) - # Prevent changing target part after creation - if self.has_field_changed('part'): - raise ValidationError({ - 'part': _('Build order part cannot be changed') - }) - try: super().save(*args, **kwargs) except InvalidMove: @@ -122,6 +116,17 @@ class Build(MPTTModel, InvenTree.mixins.DiffMixin, InvenTree.models.InvenTreeBar 'parent': _('Invalid choice for parent build'), }) + def clean(self): + """Validate the BuildOrder model""" + + super().clean() + + # Prevent changing target part after creation + if self.has_field_changed('part'): + raise ValidationError({ + 'part': _('Build order part cannot be changed') + }) + @staticmethod def filterByDate(queryset, min_date, max_date): """Filter by 'minimum and maximum date range'. diff --git a/InvenTree/build/test_build.py b/InvenTree/build/test_build.py index d8300d0958..0961e0329d 100644 --- a/InvenTree/build/test_build.py +++ b/InvenTree/build/test_build.py @@ -491,7 +491,7 @@ class BuildTest(BuildTestBase): # Should not be able to change the part after the Build is saved with self.assertRaises(ValidationError): bo.part = assembly_2 - bo.save() + bo.clean() def test_cancel(self): """Test cancellation of the build"""