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