Summarizing Inside a Django Template

I have the following template in django, I want to get the results of the last two columns for each of the 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 %}

All this is done using the following models, simplified for this issue.

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 apologize for the bad English :)

+3
source share
1 answer

From my experience with Django, I would say that these things are not easy to do in the template. I try to do my calculations in a view instead of a template.

My recommendation would be to calculate the two amounts you need in the view instead of the template.

, , . :

<td>{% documento.cuentasxdocumento_set.all | sum_monto:"pos" %}</td>
<td>{% documento.cuentasxdocumento_set.all | sum_monto:"neg" %}</td>

, , , , . , sum_monto .

:

from django import template

register = template.Library()

@register.filter
def sum_monto(cuentas, op):
    if op == "pos":
         return sum(c.monto for c in cuentas if c.monto > 0)
    else
         return sum(c.monto for c in cuentas if c.monto < 0)
+2

Source: https://habr.com/ru/post/1765553/


All Articles