The Django aggregate functions usually could be translated into corresponding SQL phrases, so the result would be an SQL query. SQL has AVG
, or SUM
, but doesn't have PRODUCT
(Why is there no PRODUCT aggregate function in SQL?). One reason for this is probably the overflow danger. If you start to multiply even smaller numbers together you can pretty quickly arrive to very large values.
So if the computation will happen in Python land anyway (as opposed to SQL land under the hood of your SQL engine) then you can do something like:
import numpy as np
ratios = MyModel.objects.filter(ratio__lte=10).values_list("ratio", flat=True)
mys = np.prod(ratios)
Someone may try to implement a Product Django aggregate with a SELECT exp(SUM(LOG(price))) FROM products;
type mathematical trick, but concluding to a product using exponential and logarithmic functions sounds like to me blatant waste of computational resources. I like to keep things in SQL level if possible (so the engine can optimize also), but maybe this is an example where it'd make sense to bring it to Python?