Monday, March 8, 2021

Re: Sum total by date

CODE NOT TESTED
You can do this in the get_context_data method, in the view:
from django.db.models import Sum

chart_data = {}
for date in Expense.objects.all().distinct('date').values_list('date', flat=True):
  chart_data[date] = Expense.objects.filter(date=date).aggregate(Sum('amount'))['total__sum']
context['chart_data'] = chart_data

and this in the related template:
{% for key, value in chart_data.items %}
  <tr>
    <td>{{ key }}</td>
    <td>{{ value }}</td>
  </tr>
{% endfor %}

Il giorno martedì 2 marzo 2021 alle 01:16:50 UTC+1 linsa...@gmail.com ha scritto:

My Model

class Expense(models.Model):
    date = models.DateField("Date", default=now)
    department =  models.CharField( max_length=200)
    #employee = models.ForeignKey(to=User, on_delete=models.CASCADE)
    employee = models.CharField( max_length=200)
    transactionId = models.CharField(max_length=200, unique=True)
    institutions = models.CharField( max_length=200)
    description = models.TextField(max_length=200)
    amount = models.FloatField()
    account_dr =  models.CharField( max_length=200)
    account_cr =  models.CharField( max_length=200)


The normal display of Transactions but i want a summary for each date like the table below and how to represent it on the html template
Capture.PNG
This is the result i want to achieve
Capture1.PNG

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/25c80ab0-89ea-4f40-a5a0-8346474337ben%40googlegroups.com.

No comments:

Post a Comment