I wanted to calculate business days in Django annotate. For example, if an event was generated 7 days back, and I wanted to know how many business days had passed. As per the example, 7 days includes [Monday - Sunday], and I only wanted to include [Monday - Friday], which means 5 business days. I've done this logic via some pythonic hack, but I wanted to do this in Django annotate() method. So I can filter the results based on business days.
Here is an example of what I've done so far:
table_values = Table.objects.all().annotate(issue_severity=datetime.utcnow() - F("event__created"))
for table in table_values:
date = datetime.now(timezone.utc) - table.issue_severity
dates = (date + timedelta(x + 1) for x in range(table.issue_severity.days))
table.business_days = sum(day.weekday() < 5 for day in dates)