I have the following template in django, i want to get the totals of the last 2 columns for each of my document objects
{% for documento in documentos %}
    {% for cuenta in documento.cuentasxdocumento_set.all %}
        <tr {% cycle 'class="gray"' '' %} >
            {% if forloop.first %}
                    <td>{{ documento.fecha_creacion.date }}</td>
                    <td>{{ cuenta.cuenta.nombre }}</td>
                    <td>
                        {% if cuenta.monto >= 0 %}
                            {{ cuenta.monto}}
                        {% endif %}
                    </td>
                    <td>
                        {% if cuenta.monto <= 0 %}
                            {{ cuenta.monto }}
                        {% endif %}
                    </td>
            {% else %}
                    <td colspan="4"></td>
                    <td>{{ cuenta.cuenta.codigo }}</td>
                    <td>{{ cuenta.cuenta.nombre }}</td>
                    <td>
                        {% if cuenta.monto <= 0 %}
                            {{ cuenta.monto }}
                        {% endif %}
                    </td>
                    <td>
                        {% if cuenta.monto >= 0 %}
                            {{ cuenta.monto }}
                        {% endif %}
                    </td>
            {% endif %}
        </tr>
    {% endfor %}
    <tr>
        <td colspan="1"></td>
        <td>Document Total</td>
        <td></td>
        <td></td>
    </tr>
{% endfor %}
This is all done using the following models, which are simplified for the purpose of this question
class Documento(models.Model):
    numero_impreso = models.CharField(max_length=50)
    fecha_creacion = models.DateTimeField(auto_now_add = True)
    cuentas = models.ManyToManyField('CuentaContable', through = 'CuentasXDocumento', null = True)
    def __unicode__(self):
        return self.tipo.nombre + ": " + self.numero_impreso
class CuentasXDocumento(models.Model):
    cuenta = models.ForeignKey('CuentaContable')
    documento = models.ForeignKey('Documento')
    monto = models.DecimalField(max_digits= 14, decimal_places = 6)
    linea = models.IntegerField()
class CuentaContable(models.Model):
    codigo = models.CharField(max_length=50)
    nombre = models.CharField(max_length=100)    
    def __unicode__(self):
        return self.nombre
Finally I'm sorry for the bad english :)