The excellent ArrayAgg and StringAgg Postgres-specific aggregates were introduced in contrib.postgres in Django 1.9 and I've been quite happy using them here and there.
Thanks for the keeping Django awesome!
The
documentation of Postgres 9.0 first mentions the possiblity of ordering the results within such aggregations.
This could be useful in some cases.
For example: it could make sense to perform the StringAgg in lexicographical order in some cases.
The simple format of ordering the aggregation result in SQL is quite simple:
SELECT ARRAY_AGG(some_field ORDER BY some_field ASC) FROM table;
SELECT ARRAY_AGG(some_field ORDER BY some_field DESC) FROM table;
SELECT ARRAY_AGG(some_field ORDER BY other_field ASC) FROM table;
It would be nice if the above would be supported as follows:
Model.objects.aggregate(ArrayAgg(some_field, order_by='some_field'))
Model.objects.aggregate(ArrayAgg(some_field, order_by='-some_field'))
Model.objects.aggregate(ArrayAgg(some_field, order_by='other_field'))
where order_by is an optional parameter. If it not specified, behavior can remain unchanged from the current implementation.
Note that the documentation for Postgres >= 9.3 also mentions json_agg ordering and Postgres >= 9.4 mentions json_object_agg as well.
I'm unsure whether these should be included as well as I couldn't find the matching implementation in the ORM atm and have no knowledge on plans in that direction.