﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31469	Custom lookup ignored inside built-in aggregation.	Oliver Ford	nobody	"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."	New feature	closed	Database layer (models, ORM)	3.0	Normal	duplicate	custom lookup,aggregation		Unreviewed	0	0	0	0	0	0
