How to aggregate the average of a calculation based on two columns?
问题 I want to write a Django query to give me the average across all rows in my table. My model looks like class StatByDow(models.Model): total_score = models.DecimalField(default=0, max_digits=12, decimal_places=2) num_articles = models.IntegerField(default=0) day_of_week = IntegerField( null=True, validators=[ MaxValueValidator(6), MinValueValidator(0) ] ) and I attempt to calculate the average like this everything_avg = StatByDow.objects.all().aggregate(Avg(Func(F('total_score') / F('num