Opened 4 years ago

Last modified 4 years ago

#31829 closed Bug

Chaining KeyTransform with 'contains' lookup uses builtin lookup instead of overridden lookup — at Version 1

Reported by: Sage Abdullah Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Sage Abdullah)

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".

Change History (1)

comment:1 by Sage Abdullah, 4 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top