@Josh, thanks for pointing out the Expressions API

I will look into it and see if I can come up with a solution that looks 
reasonable (including an investigation into requirements on contributing to 
the GROUP BY) and report here / in Trac.


On Monday, March 7, 2016 at 1:14:06 PM UTC+1, Tim Graham wrote:
>
> Ticket for JSON_AGG: https://code.djangoproject.com/ticket/26327
>
> On Thursday, January 7, 2016 at 5:45:03 PM UTC-5, Josh Smeaton wrote:
>>
>> Seems reasonable enough to me. Expressions already support generating an 
>> ORDER BY clause by calling .asc() or .desc() on them. That'd allow your 
>> proposed API to support:
>>
>> Model.objects.aggregate(ArrayAgg(some_field, order_by=
>> F('some_field').asc()))
>>
>> Or any other expression. Consider that any ordering added within *may* 
>> need to be contributed to GROUP BY, but I haven't read the documentation to 
>> say that is a requirement.
>>
>> Feel free to open a ticket in Trac to track the feature. If you're also 
>> up for implementing it (..and testing.. and documenting..) I'm quietly 
>> confident that it'll be fairly straight forward to add, given familiarity 
>> with Expressions API.
>>
>> Additionally, if you think json_agg and json_object_agg should be 
>> implemented, you can open a feature ticket on Trac for those too. 
>>
>> Regards,
>>
>> On Friday, 8 January 2016 00:46:52 UTC+11, Floris den Hengst wrote:
>>>
>>> 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 
>>> <http://www.postgresql.org/docs/9.0/static/functions-aggregate.html> 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.
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/5abe66e1-3ddb-41fa-86fa-f8e1007405a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to