在ListView中,我只能从列表中当前查看的对象计算部分值:
def get_context_data(self, **kwargs):
""" aggiunge il totale ore """
context = super(OraListView, self).get_context_data(**kwargs)
x= self.object_list.aggregate(Sum('durata'))
context['totale_durata'] = x.get('durata__sum')
return context
如何在管理员中获得相同的结果?
class TimeAdmin(admin.ModelAdmin):
def get_total(self):
# context = super(OreAdmin, self).get_context_data(**kwargs)
# x= self.object_list.aggregate(Sum('durata'))
# total=x.get('durata__sum')
total="aaaaaaaa"
return total
def changelist_view(self, request, extra_context=None):
my_context = {
'total': self.get_total(),
}
return super(TimeAdmin, self).changelist_view(request,
extra_context=my_context)
admin_中似乎没有object_list。
答案 0 :(得分:1)
您需要额外的步骤和额外的课程:
from django.contrib.admin.views.main import ChangeList
class TimeChangeList(ChangeList):
def get_results(self, request):
super(TimeChangeList, self).get_results(request)
totals = self.result_list.aggregate(Sum('durata'))
self.totale_durata = totals['durata__sum']
class TimeAdmin(admin.ModelAdmin):
def get_changelist(self, request):
return TimeChangeList
changelist.html:
{{ cl.totale_durata|default:0 }