How to compute average of an aggregate in django
<p>If I have an aggregate, can I get the average of the values in the query, without computing it in python memory?</p>

<pre><code>from django.db.models import F, Sum, FloatField, Avg
Model.objects.filter(...)\
    .values('id')\
    .annotate(subtotal=Sum(...math here...), output_field=FloatField())\
    .annotate(total=Avg(F('subtotal'))) #this line throws a FieldError
</code></pre>

<p>Is there any way to get the <code>Avg</code> of the <code>subtotal</code> values in the query? It gives me an error that I'm not allowed to compute <code>Avg</code> on an aggregate ("<code>subtotal</code>"), but I can't replace the <code>.values('id')</code> grouping because the <code>.annotate(...math here...)</code> operations inside aren't <a href="https://en.wikipedia.org/wiki/Distributive_property" rel="noreferrer">distributive</a> accross <code>Model</code> objects.</p>