Does anybody are conscious of the way i would, with the django ORM, create a query that conditionally aggregated related models?
Let us say, for instance, that you simply operate a site that sells stuff, and you'd like to learn just how much each worker has offered within the last 7 days. It is rather simple enough to get this done total sales:
q = Employee.objects.filter(type='salesman').annotate(total_sales = models.Sum('sale__total'))
presuming Worker and Purchase models having a many-to-many relationship together. OK, however wouldso would Time passes about constraining this to any or all sales during the last 7 days (or any arbitrary time period)? Does anybody know?
Okay, I suppose I did not think this through very far. I did not understand that filter handled things having a left join (though thinking onto it, how else wouldn't it map towards the db?), therefore the apparent response is:
Employee.objects.filter(type='salesman').filter(sale__timestamp__gte = start_date)\ .exclude(sale__timestamp__gte = end_date).annotate(...