[Django] #31469: Custom lookup ignored inside built-in aggregation

8 views
Skip to first unread message

Django

unread,
Apr 15, 2020, 7:54:56 AM4/15/20
to django-...@googlegroups.com
#31469: Custom lookup ignored inside built-in aggregation
-------------------------------------+-------------------------------------
Reporter: Oliver | Owner: nobody
Ford |
Type: Bug | Status: new
Component: Database | Version: 3.0
layer (models, ORM) | Keywords: custom
Severity: Normal | lookup,aggregation
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
With a '[[https://docs.djangoproject.com/en/3.0/howto/custom-
lookups/|custom lookup]]' such as:
{{{
@JSONField.register_lookup
class JsonbArrayLength(models.Transform):
output_field = models.IntegerField()
lookup_name = "arraylen"
function = "JSONB_ARRAY_LENGTH"
}}}

Using it as `qs.annotate(num_items=models.Sum("jsonfield__arraylen"))`
results in the query fragment:
{{{
SELECT SUM("model"."jsonfield") AS "num_items"
}}}

i.e. the `arraylen` lookup is ignored/not done.
`qs.values("jsonfield__arraylen")` works as expected.

Modifying it to `models.Sum(JsonbArrayLength("jsonfield"))` gives the
desired result.

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

Django

unread,
Apr 16, 2020, 2:10:12 AM4/16/20
to django-...@googlegroups.com
#31469: Custom lookup ignored inside built-in aggregation.
-------------------------------------+-------------------------------------
Reporter: Oliver Ford | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: custom | Triage Stage:
lookup,aggregation | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* type: Bug => New feature
* resolution: => duplicate


Comment:

Aggregates doesn't respect lookups, it's not related with a custom
implementation or with `JSONField`'s.

Duplicate of #25534.

--
Ticket URL: <https://code.djangoproject.com/ticket/31469#comment:1>

Reply all
Reply to author
Forward
0 new messages