#31829: Chaining KeyTransform with 'contains' lookup uses builtin lookup instead of overridden lookup -------------------------------------+------------------------------------- Reporter: sage | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: master (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Description changed by sage:
Old description: > The previous implementation of `django.contrib.postgres.fields.JSONField` > 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"`. New description: 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"`. -- -- Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:1> 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 django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.5c56a11524f89d5c85afd805f486c85d%40djangoproject.com.