#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.

-- 
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/049.a3a8301d2b3a87cf3552d8b9dfd8336c%40djangoproject.com.

Reply via email to