4

In Django Admin i want to display the sum of all action durations. I solved it wirh an extra field. But the sum of

01:30:00 h 03:45:00 h

are summed to 47500,0 because sum converts times to integer.

I changed my admin.py like it is described here: django-admin: Add extra row with totals

class MyChangeList(ChangeList):

    def get_results(self, *args, **kwargs):
    super(MyChangeList, self).get_results(*args, **kwargs)
    q = self.result_list.aggregate(status_sum=Sum('duration'))
    self.status_count = q['action_sum']

...

class ActionAdmin(admin.ModelAdmin):

    def get_changelist(self, request):
    return MyChangeList

class Meta:
    model = Status

list_display = ('name', 'duration')

Duration is defienes as TimeField in modles.py:

class Action (models.Model):

    duration = models.TimeField() 

Somebody knows how to change aggregate() function in MyChangeList ? I think i have to change time values to float or integer, make the sum and then convert it back.

Any suggestion?

Thanks a lot.

Community
  • 1
  • 1
surfi
  • 1,451
  • 2
  • 12
  • 25

1 Answers1

3

I suppose you are using MySQL. It has an old bug on summing up times (I don't know if it's fixed now).

You can store the duration in minutes (or 15-minutes if it's ok). Or you can use django-durationfield.

DrTyrsa
  • 31,014
  • 7
  • 86
  • 86
  • I'm using Mysql. I think it's the bug with mysql you mentioned. Thanks a lot, i installed **django-durationfield** and changed the last two lines in MyChangeList to: class MyChangeList(ChangeList): ... q = self.result_list.aggregate(status_sum=Sum('duration')) self.status_count = timedelta(microseconds=q['action_sum']) Now it works ;-) – surfi Jan 16 '12 at 12:36