I'm annotating the difference between two dates then aggregating the rounded average. The problem is that the difference between the two dates is a datetime.timedelta
so I am getting the error:
django.db.utils.ProgrammingError: cannot cast type interval to numeric
How can can I use the integer days_to_pay.days
in the Avg()
?
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('days_to_pay')),
)
I've tried specifying the output field on the annotation a models.IntegerField()
but it causes:
TypeError: float() argument must be a string or a number, not 'datetime.timedelta'