From 4db8d1a63b0ce5d2bf4e9b67bdd713edba43656c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Mon, 11 Nov 2024 00:26:51 +0100 Subject: [PATCH] Reset converted_price when currency or price changes --- games/models.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/games/models.py b/games/models.py index 18cab6f..89cf3a6 100644 --- a/games/models.py +++ b/games/models.py @@ -164,6 +164,16 @@ class Purchase(models.Model): raise ValidationError( f"{self.get_type_display()} must have a related purchase." ) + if self.pk is not None: + # Retrieve the existing instance from the database + existing_purchase = Purchase.objects.get(pk=self.pk) + # If price has changed, reset converted fields + if ( + existing_purchase.price != self.price + or existing_purchase.price_currency != self.price_currency + ): + self.converted_price = None + self.converted_currency = None super().save(*args, **kwargs)