#33373: Canont use expression as Right Hand Side of JSONField has_keys lookup
-------------------------------------+-------------------------------------
Reporter: john- | Owner: nobody
parton |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33373>
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/054.87ec57bebd0f7d54a68eb5d0aa159d05%40djangoproject.com.