{{{#!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',
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, JSONBAgg)
pass
from myapp.aggregates import OrderableJSONBAgg as JSONBAgg
}}}
I believe adding that mixin is all that would be required to add this
functionality.
--
Ticket URL: <https://code.djangoproject.com/ticket/31691>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
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, JSONBAgg)
pass
from myapp.aggregates import OrderableJSONBAgg as JSONBAgg
}}}
I believe adding that mixin is all that would be required to add this
functionality.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/31691#comment:1>
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, JSONBAgg)
> pass
>
> 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 adding that mixin is all that would be required to add this
functionality.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/31691#comment:2>
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>
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 replacing the existing JSONBAgg with this implementation would
> add the feature, with no backwards compat issues.
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.
I would be happy to submit a pull request.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/31691#comment:4>
* owner: nobody => john-parton
* status: new => assigned
Comment:
Sure. I think I can handle it.
--
Ticket URL: <https://code.djangoproject.com/ticket/31691#comment:6>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31691#comment:7>