From 40c74c75638de29ecab1a33c04cb50677f93dc55 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 24 Feb 2023 07:00:00 +1100 Subject: [PATCH] Allow pricing updates when PartPricing object does not yet exist (#4402) - Previously if the Part did not have a referenced PartPricing object, the schedule_pricing_update method would fail - Required a PartPricing object to actually exist (i.e. be manually created) - This patch fixes a logic error which resulted in updating being skipped if a PartPricing instance did not already exist (cherry picked from commit d8f82834eba0cb0ac9342a256973ebd02aa1daea) --- InvenTree/part/models.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 96ffca62a4..794b8e58bd 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1718,10 +1718,8 @@ class Part(InvenTreeBarcodeMixin, MetadataMixin, MPTTModel): Ref: https://github.com/inventree/InvenTree/pull/3986 """ - try: - self.pricing.schedule_for_update() - except (PartPricing.DoesNotExist, IntegrityError): - pass + pricing = self.pricing + pricing.schedule_for_update() def get_price_info(self, quantity=1, buy=True, bom=True, internal=False): """Return a simplified pricing string for this part. @@ -2295,9 +2293,11 @@ class PartPricing(common.models.MetaMixin): """Schedule this pricing to be updated""" try: - self.refresh_from_db() + if self.pk: + self.refresh_from_db() except (PartPricing.DoesNotExist, IntegrityError): # Error thrown if this PartPricing instance has already been removed + logger.warning(f"Error refreshing PartPricing instance for part '{self.part}'") return # Ensure that the referenced part still exists in the database @@ -2305,6 +2305,7 @@ class PartPricing(common.models.MetaMixin): p = self.part p.refresh_from_db() except IntegrityError: + logger.error(f"Could not update PartPricing as Part '{self.part}' does not exist") return if self.scheduled_for_update: @@ -2322,6 +2323,7 @@ class PartPricing(common.models.MetaMixin): self.save() except IntegrityError: # An IntegrityError here likely indicates that the referenced part has already been deleted + logger.error(f"Could not save PartPricing for part '{self.part}' to the database") return import part.tasks as part_tasks