2

Suppose, I have a Django model called MyModel like this:

class MyModel(models.Model):
    event_started = models.DateTimeField(default=timezone.now)
    event_duration = models.DurationField()

Now I want to get a queryset with the total seconds grouped by day. The following code returns the queryset with the total duration as timedelta instead of seconds:

from django.db.models import Sum

qs = (MyModel.objects
      .annotate(day=TruncDay('event_started'))
      .values('day')
      .annotate(total_duration=Sum('event_duration'))
      .order_by('-day')
      )

I want to include the total seconds instead of timedelta in the total_duration field of the queryset. How can I do that?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy