Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Python
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.
Posted
Updated 9-Jul-22 22:15pm
Comments
Member 15627495 10-Jul-22 1:15am    
I'm new to python,
but the problem could come from a bad function use,
maybe from .all() , or from aggregate()

verify what do each of this function.
_total = 0
.all(_total = _total + Sum('payout'))['payout__sum']) could be enough to reach your goal.

as agregate() gather all rows.

1 solution

You are looping all on all the
{% for finances in viewpayout %}
objects hence it's going to render multiple of course.

Try below:

def finances(request):
        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
        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 render(request, 'portal/finances.html', {"updatepayout": updatepayout, "updatewithdraw": updatewithdraw, "updatecash": updatecash, "viewpayout": viewpayout, 'totalbank':totalbank, 'totalcash':totalcash})




<pre><li class="list-group-item active" aria-current="true">Finances</li>
  Cash Available In Bank: {{ totalbank }}
  Cash Available On Hand: {{ totalcash }}
  </ul>
 
Share this answer
 
v2
Comments
Richard MacCutchan 10-Jul-22 7:49am    
totalbank = totalwithdraw - totalcash

Are you sure that should not be
totalcash -= totalwithdraw

??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900