问题
I have three models the first is an account model the second is a subscription which is my cutomised model while the next is payment. Account model has a balance field I want to get the balance of each user in django admin panel byquering the total amount of payments done in the payments model minus the total subscription for each user differently .and display their balances. I am a begginner in django help me map this
class Account(AbstractBaseUser):
username = models.CharField(max_length=30, )
farmernumber =models.CharField(max_length=3,)
farmerlocation =models.CharField(max_length=3,choices=location)
idno =models.CharField(max_length=9)
firstname =models.CharField(max_length=10)
secondname = models.CharField(max_length=10)
phonenumber =models.CharField( max_length=10)
balance = models.DecimalField(max_digits=10, decimal_places=2,default=00)
#def balance(self):
#balance will be totl paid in the payment app if there are no payments then its-deductions_total()
#balance is total paid by that user in the payments model-subscrption.deduction_tota()
#save the balance in the account.balance field
#this is where the problem lies have been in the internet tried all sorts of queries please help me
class Payment(models.Model):
user= models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_paid= models.DateTimeField(auto_now_add=True, verbose_name="payment time")
payment_method=models.CharField(max_length=5,choices=method)
receptnumber=
amount= models.IntegerField( verbose_name="amount paid")
description=models.TextField(max_length=300)
def paid_total(self):
class subscription(models.Model):
month=models.CharField(max_length=3, choices=month)
timestamp =models.DateField(auto_now_add=True)
fee=models.DecimalField(decimal_places=1,default=0,max_digits=3)
def __str__(self):
return self.month
@classmethod
def deduction_total(cls):
return cls.objects.aggregate(deduction=Sum('fee'))
来源:https://stackoverflow.com/questions/63445897/how-to-compute-balance-for-each-user-separately-as-a-field-in-django