File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55@admin .register (Customer )
66class CustomerAdmin (admin .ModelAdmin ):
7- pass
7+ list_display = [ 'user' , 'current_amount' ]
88
99
1010@admin .register (Transaction )
Original file line number Diff line number Diff line change @@ -11,6 +11,15 @@ class Customer(models.Model):
1111 def __str__ (self ):
1212 return f"{ self .user } "
1313
14+ def re_calculate_current_amount (self ):
15+ self .current_amount = 0
16+ for t in self .transaction_set .all ():
17+ if t .type == Transaction .Type .INCOME :
18+ self .current_amount += t .amount
19+ else :
20+ self .current_amount -= t .amount
21+ self .save ()
22+
1423
1524class Transaction (models .Model ):
1625
@@ -26,3 +35,7 @@ class Type(models.TextChoices):
2635
2736 def __str__ (self ):
2837 return f"{ self .type } [{ self .amount } ]"
38+
39+ def save (self , * args , ** kwargs ):
40+ super ().save (* args , ** kwargs )
41+ self .customer .re_calculate_current_amount ()
You can’t perform that action at this time.
0 commit comments