Fix updating of snapshot values
This commit is contained in:
parent
0184cba30d
commit
411d258cd7
4 changed files with 22 additions and 23 deletions
|
@ -159,10 +159,15 @@ class Snapshot(AccountModel):
|
|||
trans.save()
|
||||
|
||||
self.diff = self.value - self.start_value
|
||||
self.sum = (
|
||||
self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0) or 0
|
||||
)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def update_sum(self):
|
||||
self.sum = self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0)
|
||||
self.sum = (
|
||||
self.transaction_set.aggregate(sum=models.Sum("value")).get("sum", 0) or 0
|
||||
)
|
||||
super().save()
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
|
@ -219,10 +224,14 @@ class Transaction(CustomModel):
|
|||
)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
prev_self = Transaction.objects.get(pk=self.pk)
|
||||
if Transaction.objects.filter(pk=self.pk):
|
||||
prev_self = Transaction.objects.get(pk=self.pk)
|
||||
else:
|
||||
prev_self = None
|
||||
self.account = self.snapshot.account
|
||||
super().save(*args, **kwargs)
|
||||
prev_self.snapshot.update_sum()
|
||||
if prev_self is not None:
|
||||
prev_self.snapshot.update_sum()
|
||||
self.snapshot.update_sum()
|
||||
|
||||
def __str__(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue