代码之家  ›  专栏  ›  技术社区  ›  armonge

在Django模板中汇总

  •  3
  • armonge  · 技术社区  · 14 年前

    我在django中有下面的模板,我想得到每个文档对象的最后2列的总数

    {% 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 %}
    

    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
    

    最后,我为糟糕的英语道歉:)

    1 回复  |  直到 14 年前
        1
  •  2
  •   Arlaharen    14 年前

    我的建议是在视图而不是模板中计算所需的两个总和。

    也就是说,可以使用 custom filters and tags . 使用过滤器时,它可能如下所示:

    <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)