Anyone have advice on this? On Dec 20, 2:54 am, yummy_droid <yus...@gmail.com> wrote: > Hi, > > I wanted a "sum" of a particular column being listed in theadminlist > page. I looked it up and did it the following way. Is this the correct > way of doing it? > > I modified theadmin.py to do the calculation: > > class CollectionAdmin(admin.ModelAdmin): > list_display = ('supplier', 'collected_date', 'tonnage',) > list_filter = ('collected_date',) > > defchangelist_view(self, request, extra_context=None): > from django.contrib.admin.views.main import ChangeList > from django.db.models import Sum > if extra_context is None: > extra_context = {} > mycl = ChangeList(request, self.model, list(self.list_display), > self.list_display_links, self.list_filter, self.date_hierarchy, > self.search_fields, self.list_select_related, self.list_per_page, > self.list_editable, self) > fieldsum = mycl.get_query_set().aggregate(Sum('tonnage')) > extra_context['tonnage__sum'] = fieldsum['tonnage__sum'] > return super(CollectionAdmin, self).changelist_view(request, > extra_context=extra_context) > > then i added the following line to change_list.html: > > {% if tonnage__sum %}Total tonnage: {{ tonnage__sum }}{% endif > %} > > Am I doing something bad in thechangelist_viewfunction above? Or is > it the right way to do this? > > the reason I want to use theadminlist is because I like the > filtering option, and I can then see directly the sum of the column > based on whatever is currently filtered. > > Thanks.
-- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.