2

I'm trying to produce a table that will show the total maturity amounts for each financial institution that a plan has.

A plan is the term I use for person. So each person can have multiple investments. I'm having trouble creating a table that will do this correctly.

Currently I have a report that displays each investment sorted by Maturity Date, i.e. 2011,2012, etc.

At the bottom I would like to place this summary table, but my query displays duplicates of each financial institution, due to being able to have multiple investments.

My current query:

list = Investment.objects.all().filter(plan = plan).order_by('financial_institution').filter(maturity_date__gte= '%s-1-1' % current_year).distinct()

This will output:

  • TD Bank $10k
  • TD Bank $10k
  • Scotia Bank $10k
  • Etc

But I'd like:

  • TD Bank $20k
  • Scotia Bank $10k
  • Etc

1 Answer 1

5

So, you want to aggregate the values of the investments for a plan:

from django.db.models import Sum
my_plan.investment_set.filter(
    maturity_date__gte=whenever
).values('financial_institution').annotate(Sum('value'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.