Opened 5 years ago
Last modified 5 years ago
#31691 closed New feature
django.contrib.postgres.aggregates.JSONBAgg does not support ordering — at Version 2
Reported by: | john-parton | Owned by: | nobody |
---|---|---|---|
Component: | contrib.postgres | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Here's a contrived example:
class Author(models.Model): name = models.CharField(max_length=50) alias = models.CharField(max_length=50, null=True, blank=True) age = models.PositiveSmallIntegerField(default=30) class Article(models.Model): authors = models.ManyToManyField(Author, related_name='articles') title = models.CharField(max_length=50) Article.objects.annotate( authors_json=JSONBAgg( Func( Value('name'), 'name', Value('alias'), 'alias', function='jsonb_build_object' ), ordering='age' ) )
The ordering kwarg is ignored, but Postgres would have no problem understanding the aggregate with an ORDER BY clause
In my code I did the following
class OrderableJSONBAgg(OrderableAggMixin, Aggregate): function = 'JSONB_AGG' template = '%(function)s(%(expressions)s %(ordering)s)' output_field = JSONField() def convert_value(self, value, expression, connection): if not value: return [] return value from myapp.aggregates import OrderableJSONBAgg as JSONBAgg
I believe adding that mixin is all that would be required to add this functionality.
Change History (2)
comment:1 by , 5 years ago
Description: | modified (diff) |
---|
comment:2 by , 5 years ago
Description: | modified (diff) |
---|
Note:
See TracTickets
for help on using tickets.