Код: Выделить всё
class Fundraising(models.Model):
@property
def amount_raised(self):
amount_raised = FundraisingDonation.objects.filter(
fundraising_event=self,
).aggregate(donationamount=Coalesce(Sum('donationamount'), 0.00))['donationamount']
return amount_raised
class FundraisingDonation(models.Model):
donationamount = models.DecimalField(max_digits=11, decimal_places=2, default=0)
class Testmodel(models.Model):
organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE)
allocation = models.ForeignKey(Fundraising, on_delete=models.CASCADE, related_name='items')
percentshare = models.DecimalField(max_digits=11, decimal_places=2, default=0) #SET BY USER
@property
def amount(self):
amount = self.allocation.amount_raised * self.percentshare
return amount
Я пытаюсь теперь суммируем суммы для каждой организации по новой модели, однако следующее не работает, поскольку я не верю, что смогу выполнить запрос по вычисляемому полю?
Код: Выделить всё
class Totals(models.Model):
totals = total + totalB + totalC
@property
def total(self):
total = Testmodel.objects.filter(
organisation=self.organisation
).aggregate(amount=Coalesce(Sum('amount'), 0.00))['amount']
Подробнее здесь: https://stackoverflow.com/questions/618 ... ated-field
Мобильная версия