2014-12-07 92 views
0

我論文模型和管理模型顯示beloy增加額外排在Django聯模板

我想在模型交易,顯示總金額(那是量字段的總和)

有不是在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

您必須將此函數添加到事務模型中。

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

並添加函數名TransactionAdmin,模型管理的list_display選項:

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

謝謝您的回答居然是我我要的是顯示在變化形式sum_amount(當你點擊交易鏈接)我知道我可以覆蓋更改列表視圖並將總額添加到額外的上下文中,但我想要顯示總金額顯示在其將要顯示的AmountPaidInline表單中 – user2388404 2014-12-10 18:55:16

+0

簡而言之,我想在內聯中添加另一行以顯示AmountPaind模型中的總金額 – user2388404 2014-12-10 19:02:20

+0

請給我更多h elp在這 – user2388404 2014-12-16 07:10:01