# add values inside for loop

**URL:** https://forum.djangoproject.com/t/add-values-inside-for-loop/13437
**Category:** Forms & APIs
**Created:** [April 29, 2022, 12:00pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437 "2022-04-29T12:00:57Z")
**Posts on this page:** 10
**Page:** 2

<div class="post-metadata">

### Author: ![itsjamaal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/itsjamaal/32/8319_2.png) [@itsjamaal](https://forum.djangoproject.com/u/itsjamaal)
#### Post date: [April 29, 2022, 2:27pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/21 "2022-04-29T14:27:45Z")

</div>

Sorry didn’t understood the control over the data

```auto
transid = models.AutoField(primary_key=True)
	transdate = models.DateField()
	transdetails = models.CharField(max_length=25)
	transcategory = models.ForeignKey(MasterCategory, on_delete=models.CASCADE)
	transmode = models.ForeignKey(PaymentMode, on_delete=models.CASCADE)
	transsubdetails = models.CharField(max_length=100, null=True)
	transamount = models.FloatField()

```

only the formats I have mentioned here.  
While on input I am not given any formats.

Only while I design the “forms” for inputting the values from html, I should thinkoff.

As of now I don’t have any control over the data model other than mentioned in “models.py”

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [April 29, 2022, 2:35pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/22 "2022-04-29T14:35:24Z")

</div>

Ok, changing topics slightly.

You can use aggregation to calculate your totals.

For example:

```auto
totals_dict = {
    'cashbal': Daily.objects.aggregate(cashbal=Sum('transamount', filter=Q(transmode__name='Cash'))),
...

```

Once you’ve done that for all the fields, you can then pass `totals_dict` into your template through the context, accessing it from the template as `totals_dict.cashbal`.

---

<div class="post-metadata">

### Author: ![itsjamaal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/itsjamaal/32/8319_2.png) [@itsjamaal](https://forum.djangoproject.com/u/itsjamaal)
#### Post date: [April 29, 2022, 2:37pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/23 "2022-04-29T14:37:03Z")

</div>

Ok Thanks,

Let me try it and come back.

---

<div class="post-metadata">

### Author: ![itsjamaal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/itsjamaal/32/8319_2.png) [@itsjamaal](https://forum.djangoproject.com/u/itsjamaal)
#### Post date: [April 30, 2022, 4:47pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/24 "2022-04-30T16:47:05Z")

</div>

```auto
def testing(request):
	totals_dict = {
		'Cash_Bal': Daily.objects.aggregate(CashBAL=Sum('transamount', filter=Q(transmode__name='Cash'))), 
		'Enbd_Bal': Daily.objects.aggregate(ENBDBAL=Sum('transamount', filter=Q(transmode__name='ENBD'))),
		'NoL_Bal': Daily.objects.aggregate(NoLBAL=Sum('transamount', filter=Q(transmode__name='NoL'))), 
		'PayIT_Bal': Daily.objects.aggregate(PayITBAL=Sum('transamount', filter=Q(transmode__name='Pay IT'))),
		'Sib_Bal': Daily.objects.aggregate(SibBAL=Sum('transamount', filter=Q(transmode__name='SIB'))) 
		}
	return render(request, 'testing.html', {'totals_dict': totals_dict})

```

and my html is as below

```auto
{% block content %}
<h1>Testing Place</h1>
<style>
table, tr, td, th {
  border:2px solid black;
}
</style>
<div>
    Total Balance : {{ balance }}<br>
    Cash Balance : {{ totals_dict.cashbal }}<br>
    Enbd Balance : {{ enbdbal }} <br>
    NoL Balance : {{ nolbal }} <br>
    Pay IT Balance : {{ payitbal }} <br>
    Sib Balance : {{ sibbal }} <br>
</div>
<table style="width: 100%">
    {{ totals_dict }}
</table>
{% endblock %}

```

and my output is as below

```auto
Total Balance :
Cash Balance :
Enbd Balance :
NoL Balance :
Pay IT Balance :
Sib Balance :
{'Cash_Bal': {'CashBAL': 384.5}, 'Enbd_Bal': {'ENBDBAL': 7438.84}, 'NoL_Bal': {'NoLBAL': 77.5}, 'PayIT_Bal': {'PayITBAL': 0.0}, 'Sib_Bal': {'SibBAL': 0.31999999999941053}}

```

Please guide me to correct this

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [April 30, 2022, 7:18pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/25 "2022-04-30T19:18:23Z")

</div>

Don’t create another dict in your render call. Totals\_dict is already a dict, pass it directly as the third parameter.

---

<div class="post-metadata">

### Author: ![itsjamaal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/itsjamaal/32/8319_2.png) [@itsjamaal](https://forum.djangoproject.com/u/itsjamaal)
#### Post date: [May 1, 2022, 6:24am UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/26 "2022-05-01T06:24:19Z")

</div>

```auto
def testing(request):
	totals_dict = {
		'Total_Bal': Daily.objects.aggregate(TotBal=Sum('transamount')),
		'Cash_Bal': Daily.objects.aggregate(CashBAL=Sum('transamount', filter=Q(transmode__name='Cash'))), 
		'Enbd_Bal': Daily.objects.aggregate(ENBDBAL=Sum('transamount', filter=Q(transmode__name='ENBD'))),
		'NoL_Bal': Daily.objects.aggregate(NoLBAL=Sum('transamount', filter=Q(transmode__name='NoL'))), 
		'PayIT_Bal': Daily.objects.aggregate(PayITBAL=Sum('transamount', filter=Q(transmode__name='Pay IT'))),
		'Sib_Bal': Daily.objects.aggregate(SibBAL=Sum('transamount', filter=Q(transmode__name='SIB'))) 
		}
	return render(request, 'testing.html', totals_dict)

```

and my html is as below

```auto
<div>
    Total Balance : {{ Total_Bal }}<br>
    Cash Balance : {{ Cash_Bal }}<br>
    Enbd Balance : {{ Enbd_Bal }} <br>
    NoL Balance : {{ NoL_Bal }} <br>
    Pay IT Balance : {{ PayIT_Bal }} <br>
    Sib Balance : {{ Sib_Bal }} <br>
</div>

```

```auto
Total Balance : {'TotBal': 7901.1600000000035}
Cash Balance : {'CashBAL': 384.5}
Enbd Balance : {'ENBDBAL': 7438.84}
NoL Balance : {'NoLBAL': 77.5}
Pay IT Balance : {'PayITBAL': 0.0}
Sib Balance : {'SibBAL': 0.31999999999941053}

```

and my output is as above, still I am missing something in formatting, and the output also has extra “{ }”.

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [May 1, 2022, 11:44am UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/27 "2022-05-01T11:44:04Z")

</div>

You’ve got at least four different options here:

- The queries can be modified

- Totals\_dict can be restructured

- The `render` call can be restructured

- The template can be modified.

Any _one_ of those options can be used to obtain the desired results.

As a side note, it’s become apparent to me that you’re working with “money” as a fundamental type. You never, _never_, **never** want to use a FloatField when working with currency values. The only valid approach to working with money is to use a DecimalField.

---

<div class="post-metadata">

### Author: ![itsjamaal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/itsjamaal/32/8319_2.png) [@itsjamaal](https://forum.djangoproject.com/u/itsjamaal)
#### Post date: [May 1, 2022, 11:53am UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/28 "2022-05-01T11:53:03Z")

</div>

I am having negative ( Incomes & Expenses) and positive values, initially I tried DecimalField, it throwed error while saving, that’s why I changed to FloatField.

Let me try it back again and will update.

I am just trying to record daily expenses ( personal ).

Add Values in my own form  
Get reporting done by filters like payment mode.  
Also I need reporting based on Months.  
Multiple page reports having Header grouped by months, month vise totals, totals based on payment mode.

Still need to go long, meanwhile I want to learn compact coding, mine looks very messy for me.

Thanks for your continues support.

About the four different suggestions, please show me some more light, How I can modify or restructure.

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [May 1, 2022, 12:02pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/29 "2022-05-01T12:02:09Z")

</div>

> [@itsjamaal](#):
>
> I am having negative ( Incomes & Expenses) and positive values, initially I tried DecimalField, it throwed error while saving, that’s why I changed to FloatField.

You need to fix those errors, not change your results to something that’s fundamentally wrong.

> [@KenWhitesell](#):
>
> The queries can be modified

Play with your queries in the shell.

Refer back to the work you did in the Django tutorial, part 2. ([Writing your first Django app, part 2 | Django documentation | Django](https://docs.djangoproject.com/en/4.0/intro/tutorial02/#playing-with-the-api))

What do your queries return? How can you change your query to return what you want?

> [@KenWhitesell](#):
>
> Totals\_dict can be restructured

Again, play with this in the shell. Look at what `totals_dict` looks like. What can you do to change the structure?

> [@KenWhitesell](#):
>
> - The `render` call can be restructured

In general Python terms ( **not** Django terms - this has nothing to do specifically with Django), what does `**kwargs` represent in a function call?

> [@KenWhitesell](#):
>
> The template can be modified.

How do you access an element of a dictionary in your template?

> [@itsjamaal](#):
>
> Still need to go long, meanwhile I want to learn compact coding, mine looks very messy for me.

Compact is not always better. Yes, this code can be shortened even further - and some might call it “better” - but doing so may make it less clear. (e.g. You can apply multiple aggregates in one aggregate function call.)

Never forget that Django is just Python. Anything you can do in Python can be done in Django. If Django doesn’t appear to provide a function or feature that would be helpful to you, look at the wider Python library.

---

<div class="post-metadata">

### Author: ![itsjamaal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/itsjamaal/32/8319_2.png) [@itsjamaal](https://forum.djangoproject.com/u/itsjamaal)
#### Post date: [May 1, 2022, 12:14pm UTC](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437/30 "2022-05-01T12:14:18Z")

</div>

So Much Thanks,

Given a elaborated reply ( guide ).  
I will start learning what all you have mentioned.  
Once I have fine tuned my project, will post back for your guidance.

[Previous page](https://forum.djangoproject.com/t/add-values-inside-for-loop/13437.md?page=1)
