2011-11-01 2 views
0

Je rencontre un petit problème avec ma fonction annotate (sum()). Maintenant, ce que je veux faire, c'est montrer un total pour toutes les échéances dans le plan donné, dans la liste. Lequel pour le premier il le fait. Code ci-dessous:annotate() ajoutant plus que nécessaire

#get the invesments maturing this year 
     for p in plans: 
      cur_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = current_year) 
      nxt_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = next_yr) 
      thr_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = thr_yr) 
      fr_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = fr_yr) 
      fv_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = fv_yr) 

      for inv in cur_inv: 
       total += inv.amount or 0 
      for inv in cur_inv: 
       total_m += inv.maturity_amount or 0 

      for inv in nxt_inv: 
       total2 += inv.amount or 0 
      for inv in nxt_inv: 
       total_m2 += inv.maturity_amount or 0 

      for inv in thr_inv: 
       total3 += inv.amount or 0 
      for inv in thr_inv: 
       total_m3 += inv.maturity_amount or 0 

      for inv in fr_inv: 
       total4 += inv.amount or 0 
      for inv in fr_inv: 
       total_m4 += inv.maturity_amount or 0 

      for inv in fv_inv: 
       total5 += inv.amount or 0 
      for inv in fv_inv: 
       total_m5 += inv.maturity_amount or 0 

#Calculate the holding totals with each company 
      total_list = p.investment_set.filter(maturity_date__gte= '%s-1-1' % current_year).values('financial_institution__abbr').annotate(Sum('maturity_amount')).order_by('financial_institution__abbr') 
      gtotal = total_m + total_m2 + total_m3 + total_m4 + total_m5 

      plan_list.append({ 
       'plan':p, 
       'investment': cur_inv, 
       'nxt_inv': nxt_inv, 
       'thr_inv': thr_inv, 
       'fr_inv': fr_inv, 
       'fv_inv': fv_inv, 
       'total_list': total_list, 
       'grand': gtotal, 
      }) 

Mon seul problème maintenant, est que quand il va au prochain plan, il continue à ajouter au grand total, au lieu de retourner à 0.

Suis-je manque quelque chose?

Toute aide serait appréciée.

Merci

+1

Vous semblez avoir trop écrit votre code pour vous aider correctement. D'où viennent les variables 'total_m *'? –

+0

@ChrisPratt Je vais le mettre à jour. – TheLifeOfSteve

Répondre

1

Vous utilisez += avec les total_m* vars, mais ne les a jamais remise à 0 dans votre boucle. Ils ne sont pas automatiquement réinitialisés, simplement parce qu'une nouvelle itération a commencé. FWIW, vous devriez essayer d'optimiser votre code ici. Vous générez des requêtes 6 * len (plans), ce qui peut être plutôt coûteux.

+0

Merci, ça a marché ... et je chercherai à optimiser mes requêtes. – TheLifeOfSteve

Questions connexes