This is confusing me.
# Cost per page
>>> from django.db.models import F, FloatField, Sum
>>> Book.objects.all().aggregate(
... price_per_page=Sum(F('price')/F('pages'), output_field=FloatField()))
{'price_per_page': 0.4470664529184653}
Does this mean:
i) Calculate the price per page for each book (F('price')/F('pages')) and then aggregate these over all books using the Sum function, or
ii) Aggregate the price using the Sum function, and also the number of pages using the Sum function, and them divide the former by the latter.
?
Interpretation (i) is what the statement seems to say but this will result in a fairly meaningless number (the sum of cost per page per book, which is dependant on the number of books and doesn't mean much, and is certainly not an average price per page or anything similar).
Interpretation (ii) would make more sense (it calculates the overall price per page over all books), but the fact that the expression (F('price')/F('pages')) is inside the Sum function suggests that this is not what is happening.
Additionally, to aid my understanding, depending on which interpretation is correct, what would the statement be to implement the other interpretation? e.g. is interpretation (i) is what the above statement is doing, how would interpretation (ii) be implmented? Or vice versa....
Thanks