﻿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
33373	Support expressions on rhs for __has_keys, __has_any_keys JSONField lookups.	john-parton	nobody	"I expected that the right hand side of the has_keys lookup for JSONField could be any expression which returns an array of text, but it appears that only python lists of strings are valid.

I think this is also an issue with 4.0, although I have only tested in on 3.2

Consider the following models.

{{{
class ProductVariant(models.Model):
    product = models.ForeignKey(
        'catalog.Product', on_delete=models.PROTECT
    )

    attributes = models.JSONField(_(""Attributes""), default=dict)
    
class Product(models.Model):
    configuration = ArrayField(
        models.TextField()
    )
}}}
    

Attempting to eval the queryset


{{{
ProductVariant.objects.filter(attributes__has_keys=F(""product__configuration""))
}}}


results in


{{{
TypeError

'Col' object is not iterable
}}}


As a workaround, here's what I currently do


{{{
# Define the custom function
class HasAllKeys(Func):
    function = None
    template = '(%(expressions)s)'
    arg_joiner = ' ?& '
    arity = 2
    output_field = BooleanField()

ProductVariant.objects.alias(
    attributes_has_configuration_keys=HasAllKeys(""attributes"", ""product__configuration"")
).filter(
    attributes_has_configuration_keys=True
)
}}}
"	New feature	closed	Database layer (models, ORM)	3.2	Normal	needsinfo			Unreviewed	0	0	0	0	0	0
