The previous implementation of django.contrib.postgres.fields.JSONField, when chained with a KeyTransform and a contains lookup, uses the overridden contains lookup that is JSON-based and not the builtin one that is pattern-based.
Using the example model in the 3.1 release notes,
class ContactInfo(models.Model):
data = models.JSONField()
obj = ContactInfo.objects.create(data={
'name': 'John',
'cities': ['London', 'Cambridge'],
'pets': {'dogs': ['Rufus', 'Meg']},
})
ContactInfo.objects.filter(
data__cities__contains='Cambridge',
)
The query returns a queryset with obj in it, which is expected.
However, the following query:
ContactInfo.objects.filter(
data__cities__contains='bridge',
)
Also returns a queryset with obj in it. Using the previous implementation, obj doesn't match the query because contains uses JSON-based containment checking. That is, it checks whether the array in data__cities contains an element that's exactly "bridge".
PR