I need some help with my problem. I have created a list of all my investments but now I need to seperate them by financial institution. I am trying to print a report out that will have all the investments for each financial institution on a seperate page but I am having trouble splitting them up.
For example what I want is:
TD Bank
– all investments for TD from my query
- next page –
Scotia Bank
-all investments for Scotia from my query
etc
The following is my views.py that has the query and adding all the information to my list.
Can anyone help me find a way to split it up so I can have them seperated by financial institution?
def confirmations_report(request, *args, **kwargs):
from investments.models import Investment, InvestmentManager
from reports.forms import ConfirmationsForm
import ho.pisa as pisa
import cStringIO as StringIO
import os.path
confirm_letter_list = []
context = {}
if request.POST:
form = ConfirmationsForm(request.POST)
if form.is_valid():
start_date = form.cleaned_data['start_date']
end_date = form.cleaned_data['end_date']
investments = Investment.objects.all().filter(contract_no = "",maturity_date__range=(start_date, end_date)).order_by('financial_institution')
for i in investments:
confirm_letter_list.append({
'fi':i.financial_institution,
'fi_address1': i.financial_institution.address1,
'fi_address2': i.financial_institution.address2,
'fi_city': i.financial_institution.city,
'fi_prov': i.financial_institution.state_prov,
'fi_country': i.financial_institution.country,
'fi_postal': i.financial_institution.postal,
'primary_owner': i.plan.get_primary_owner().member,
'sin': i.plan.get_primary_owner().member.client.sin,
'type': i.product.code,
'purchase_amount': i.amount,
'purchase_date': i.start_date,
})
context['confirmlist'] = confirm_letter_list
context['inv'] = investments
if request.POST.has_key('print_report_submit'):
context['show_report'] = True
context['mb_logo'] = os.path.join(os.path.dirname(__file__), "../../../media/images/mb_logo.jpg")
html = render_to_string('reports/admin/confirm_report_print.html', RequestContext(request,context))
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=unreceived-confirmations.pdf'
return response
else:
form = ConfirmationsForm()
context['form'] = form
return render_to_response('reports/admin/confirm_report.html', RequestContext(request, context))
Use a
defaultdictwith a list.Here is how you would use it:
Here is a simplified example:
As you can see the inner loop contains the list of dicts that you have added, so you need to adjust your loop logic accordingly.