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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
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>
* owner: nobody => sage
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:2>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/13238 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:3>
* needs_better_patch: 0 => 1
* version: master => 3.1
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:4>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"2d8dcba03aae200aaa103ec1e69f0a0038ec2f85" 2d8dcba0]:
{{{
#!CommitTicketReference repository=""
revision="2d8dcba03aae200aaa103ec1e69f0a0038ec2f85"
Fixed #31829 -- Used JSONField __contains lookup on key transforms.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:6>
Comment (by GitHub <noreply@…>):
In [changeset:"184a6eebb0ef56d5f1b1315a8e666830e37f3f81" 184a6eeb]:
{{{
#!CommitTicketReference repository=""
revision="184a6eebb0ef56d5f1b1315a8e666830e37f3f81"
Refs #31829 -- Added
DatabaseFeatures.json_key_contains_list_matching_requires_list.
CockroachDB's behavior matches PostgreSQL.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:8>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"32cb1fe1c66645671681407d47e8370b4afbfc3f" 32cb1fe1]:
{{{
#!CommitTicketReference repository=""
revision="32cb1fe1c66645671681407d47e8370b4afbfc3f"
[3.1.x] Refs #31829 -- Added
DatabaseFeatures.json_key_contains_list_matching_requires_list.
CockroachDB's behavior matches PostgreSQL.
Backport of 184a6eebb0ef56d5f1b1315a8e666830e37f3f81 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31829#comment:9>