currently the way I have the code written is that I have a model named Finances. I have an aggregate set up so it sums up all of the rows. I am trying to display the outcome of the rows, but when I do it displays a row for every database entry.
I understand that I am grabbing all with .all() in the viewpayout = Finances.objects.all() in views.py, but I am unsure of how this should be written to only display one row.
How would I display only one row that sums up all of the rows for a column? Any help is greatly appreciated!
Below is my code:
views.py
def finances(request):
viewpayout = Finances.objects.all()
updatepayout = UpdatePayout()
updatewithdraw = UpdateWithdraw()
updatecash = UpdateCash()
if request.method == "POST":
if 'add_payout' in request.POST:
updatepayout = UpdatePayout(request.POST)
if updatepayout.is_valid():
post = updatepayout.save(commit=False)
post.save()
return redirect("/finances")
else:
updatepayout = None
if 'bank_withdraw' in request.POST:
updatewithdraw = UpdateWithdraw(request.POST)
if updatewithdraw.is_valid():
post = updatewithdraw.save(commit=False)
post.save()
return redirect("/finances")
else:
updatewithdraw = None
if 'add_cash' in request.POST:
updatecash = UpdateCash(request.POST)
if updatecash.is_valid():
post = updatecash.save(commit=False)
post.save()
return redirect("/finances")
else:
updatecash = None
return render(request, 'portal/finances.html', {"updatepayout": updatepayout, "updatewithdraw": updatewithdraw, "updatecash": updatecash, "viewpayout": viewpayout})
model.py
class Finances(models.Model):
payout = models.DecimalField(null=True, max_digits=5, decimal_places=2, blank=True)
withdraw = models.DecimalField(null=True, max_digits=5, decimal_places=2, blank=True)
cash = models.DecimalField(null=True, max_digits=5, decimal_places=2, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
def __str__(self):
return self.payout + "\n" + self.withdraw + "\n" + self.cash
@property
def Calculate_bank(self):
totalpayout = Finances.objects.all().aggregate(Sum('payout'))['payout__sum'] or 0.00
totalwithdraw = Finances.objects.all().aggregate(Sum('withdraw'))['withdraw__sum'] or 0.00
totalbank = totalpayout - totalwithdraw
return totalbank
@property
def Calculate_cash(self):
totalwithdraw = Finances.objects.all().aggregate(Sum('withdraw'))['withdraw__sum'] or 0.00
totalcash = Finances.objects.all().aggregate(Sum('cash'))['cash__sum'] or 0.00
totalbank = totalwithdraw - totalcash
return totalbank
finances.html
<li class="list-group-item active" aria-current="true">Finances</li>
{% for finances in viewpayout %}
Cash Available In Bank: {{ finances.Calculate_bank }}
{% endfor %}
{% for finances in viewpayout %}
Cash Available On Hand: {{ finances.Calculate_cash }}
{% endfor %}
</ul>
This is what is output
I know that I do have 9 entries in the database, and I know that because I have 9 entries, it displays 9 entries. But if I add another it will show 10 and keep increasing. How can I display only one row?
https://i.stack.imgur.com/XL7A7.png[
^]
What I have tried:
Changing the viewfinances query from views.py, but I do not know what the query should be or if that will even work.