[Django]-Django convert timedelta to integer in annotation

5👍

Managed to get it working with ExtractDay

from django.db.models.functions import ExtractDay

def aggregate_ar_detail(self):
    queryset = self.annotate(
        days_to_pay=Case(
            When(Q(date_paid__isnull=False), then=F('date_paid') - F('date_trans')),
            default=None,
        )
    ).aggregate(
        avg_days=Round(Avg(ExtractDay('days_to_pay'))),
    )

Leave a comment