add values inside for loop

Sorry didn’t understood the control over the data

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”

Ok, changing topics slightly.

You can use aggregation to calculate your totals.

For example:

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.

Ok Thanks,

Let me try it and come back.

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

{% 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

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

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

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

<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>
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 “{ }”.

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.

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.

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

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)

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

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

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

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

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.

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.