I have this 3 models:
class Provider(models.Model):
name = models.CharField("Provider",max_length=200)
def __str__(self):
return self.name
class Contract(models.Model):
active = models.BooleanField(default=False, verbose_name="Active?")
provider = models.ForeignKey(Provider, on_delete=models.CASCADE,verbose_name="Provider")
total_to_spent = models.IntegerField(blank=True, null=True, verbose_name="Total to spend")
def __str__(self,):
return str(self.id) + '- ' + str(self.provider)
class Invoice(models.Model):
contract = models.ForeignKey(Contract, on_delete=models.CASCADE,verbose_name="Contract",related_name='ContractObject',)
value = models.IntegerField(verbose_name="Value")
def __str__(self,):
return str(self.contract)
total_to_spent in Contract.model is the amount of money i can spent in that contract
value in Invoice.model is the money related to that invoice that is then associated with the contract
My view
def ServicoView(request):
contracts = Contract.objects.all()
contract_active = Contract.objects.filter(active=True)
contratos_finish = Contract.objects.filter(active=False)
//i was trying something like this
for i in contracts:
invoices= Invoice.objects.filter(contract=contract['id']).values().aggregate(total=Sum('value '))
context = {
'contract_active': contract_active,
'contratos_finish':contratos_finish,
}
return render(request, 'dashboard_servico.html', context)
In my view i want to do the SUM of all invoices (value) related to one Contract for later compare to the total_to_spent to see if it passes the value or not