Reset converted_price when currency or price changes

This commit is contained in:
Lukáš Kucharczyk 2024-11-11 00:26:51 +01:00
parent bf9f3d5f56
commit 4db8d1a63b
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
1 changed files with 10 additions and 0 deletions

View File

@ -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)