2014-12-07 4 views
0

내가 거래 총 금액을 표시 할 모델에서 원하는장고 인라인 템플릿에 추가 행을 추가

beloy 표시 나는이 논문 모델 및 관리 모델을 가지고

이 있습니다 (금액 필드의 합은 그게 전부입니다) 인라인에서 변경 목록의 메소드가 아님

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 옵션에 함수 이름을 추가 : 나는 내가 원하는 것은 변화의 형태로 sum_amount을 표시하는 것입니다 실제로 어떤 답변에 대한

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

감사 (시 당신은 거래 링크를 클릭) 나는 변화 목록보기를 무시할 수 있고 여분의 문맥에 총 금액을 추가 할 수 있지만 표시 될 것입니다 AmountPaidInline 양식에 총 금액을 표시하고 싶습니다 – user2388404

+0

간단하고 싶습니다 AmountPaind 모델에 총계를 표시하기 위해 인라인에 다른 행을 추가하십시오 – user2388404

+0

제게 더 많은 시간을주세요 이것에 엘프 – user2388404

관련 문제