0

下部の管理パネルに請求書の合計を表示できるようにしたい請求書アプリがあります。(私はジャンゴスーツを使用しています)

ここに画像の説明を入力

Django内でこれを行う簡単な方法はありますか?

e:このような何かが動作する必要がありますか?

class InvoiceAdmin(admin.ModelAdmin):
    inlines = [InvoiceItemInline]
    list_display = ('description', 'date', 'status', 'invoice_amount')

    def invoice_amount(self, request):
        amount = InvoiceItem.objects.filter(invoice__id=request.id).values_list('price', flat=True)
        quantity = InvoiceItem.objects.filter(invoice__id=request.id).values_list('quantity', flat=True)
        total_current = amount(float) * quantity(float)
        total_amount = sum(total_current)
        return total_amount
4

1 に答える 1

0

list_displayすべての請求書がリストされているページにのみ適用されることを除いて、あなたは正しい軌道に乗っています。特定の請求書を編集するときには表示されません。

change_view特定の請求書の編集中に表示するには、ビュー関数として呼び出されるメソッドをオーバーライドする必要があると思います:
https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib. admin.ModelAdmin.change_view

このようなもの:

class InvoiceAdmin(admin.ModelAdmin):
    inlines = [InvoiceItemInline]
    list_display = ('description', 'date', 'status', 'invoice_amount')

    # You need to customise the template to make use of the new context var
    change_form_template = 'admin/myapp/extras/mymodelname_change_form.html'

    def _invoice_amount(self, invoice_id):
        # define our own method to serve both cases
        order_values = InvoiceItem.objects.filter(invoice__id=invoice_id).values_list(
            'price', 'quantity', flat=True)
        return reduce(
            lambda total, (price, qty): total + (price * qty),
            order_values,
            0
        )

    def invoice_amount(self, instance):
        # provides order totals for invoices list view
        return self._invoice_amount(instance.pk)

    def change_view(self, request, object_id, form_url='', extra_context=None):
        # provides order total on invoice change view
        extra_context = extra_context or {}
        extra_context['order_total'] = self._invoice_amount(object_id)
        return super(MyModelAdmin, self).change_view(request, object_id,
            form_url, extra_context=extra_context)

order_total送り返す変数を利用するには、変更フォーム テンプレートもオーバーライドする必要があることに注意してください。extra_context

于 2015-03-15T19:51:53.363 に答える