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'))),
)
Source:stackexchange.com