在django内联模板中添加额外的行

时间:2014-12-07 14:02:20

标签: python django admin inline

我有显示beloy的模型和管理模型

我希望在交易模型中显示总金额(即金额字段的总和)

Inline中没有更改列表的方法我该怎么做

class TransactionAdmin(admin.ModelAdmin):
    inlines = [TransactionAmountInline, AmountPaidInline, PhotosInline]
    fields = ('customer', 'title', 'description', 'created_at')
    readonly_fields = ('updated_at',)
    list_display = ('title', 'customer')


class AmountPaidInline(admin.TabularInline):
   model = AmountPaid
   extra = 0


class AmountPaid(models.Model):
     transaction = models.ForeignKey(Transactions)
     description = models.TextField(null=True)
     amount = models.DecimalField(max_digits=19, decimal_places=4)
     created_at = models.DateTimeField(auto_now_add=True)
     updated_at = models.DateTimeField(auto_now=True)

1 个答案:

答案 0 :(得分:1)

您必须将此功能添加到交易模型中。

def sum_amount (self):
    return AmountPaid.objects.filter(transaction=self).aggregate(total=Sum('amount'))['total']

将函数名称添加到TransactionAdmin的list_display选项中,模型admin:

list_display = ('title', 'customer', 'sum_amount')