In django >= 2.0 this could be achieved with
depts = Department.objects.all().annotate(
num_products=Count('product', filter=Q(product__review__time__range=["2012-01-01", "2012-01-08"]))
)
For more info read docs https://docs.djangoproject.com/en/2.0/topics/db/aggregation/#filtering-on-annotations