I would like to make a queryset filter to identify all model instance that have a given number of keys in a jsonfield of the model.
I have tried to create a custom lookup (#1) to extra the keys in the json field,
and would like to aggregate those into an array and use the __len lookup of the array to make my filter. Unfortunately, I am stuck at the aggregation that doesn't seem to work (#2).
1
class JsonKeys(Transform):
output_field = TextField()
lookup_name = 'keys'
function = 'jsonb_object_keys'
2
qs = qs.annotate(keysArray=ArrayAgg("myJsonField__keys"))
The error that I get is: HINT: You might be able to move the set-returning function into a LATERAL FROM item.
Anyone has already tried to perform this task ?