#31691: django.contrib.postgres.aggregates.JSONBAgg does not support ordering
-------------------------------------+-------------------------------------
     Reporter:  john-parton          |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  3.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by john-parton:

Old description:

> Here's a contrived example:
>
> {{{#!python
> 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
>
> {{{#!python
> 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.

New description:

 Here's a contrived example:

 {{{#!python
 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

 {{{#!python
 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 replacing the existing JSONBAgg with this implementation would
 add the feature, with no backwards compat issues.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31691#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.e169b39350e2dc5c5cfb52ea1368c469%40djangoproject.com.

Reply via email to