django tables2 - 基于过滤器的外键总和

时间:2016-03-04 17:01:11

标签: python django sum aggregate django-tables2

有没有办法根据django表过滤器对外键对象求和?

示例:

class Invoice(models.model):
     some_column_1 = models.Decimal(...)
     some_column_2 = models.Decimal(...)

class Payment(models.Model):
     invoice = models.ForeingKey(Invoice, related_name='invoice_payments')
     price = models.Decimal(...)
     date_payed = models.DateField(...)

class InvoiceTable(tables.Table):
     some_column_1 = tables.Column(accessor = some_column_1)
     payments = = tables.Column(accessor = invoice_payments)
     class Meta:
          model = Invoice

class InvoiceFilter(django_filters.FilterSet):
     date_from = django_filters.DateFilter(name="invoice__payments__date_payed", lookup_type='gte')
     date_to = django_filters.DateFilter(name="invoice__payments__date_payed", lookup_type='lte')
     model = Invoice

我需要在InvoiceTable付款列中显示此日期范围内的付款总额。有可能吗?

0 个答案:

没有答案