Q:

Django admin changelist_view

In a ListView I can calculate a partial value from only the object currently viewed in the list with:
    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

How can I get the same result in the admin?
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)

the object_list seems not available in the admin.

A:

You need an extra step and an extra class:
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 }

