[Django] #34548: Queryset JsonField filtering doesn't work as expected

1 view
Skip to first unread message

Django

unread,
May 8, 2023, 3:41:57 PM5/8/23
to django-...@googlegroups.com
#34548: Queryset JsonField filtering doesn't work as expected
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.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 |
-------------------------------------+-------------------------------------
Hello. I use Django 4.2, psycopg2 2.9.6 and postgis/postgis:15-3.3-alpine
as database in my project.

Previously json field filtering worked as expected
'extra_data__hello="world"' returned me queryset where objects extra_data
json field contained {"hello": "world"}. Now I noticed queryset is empty.
After some manipulations with query it start work after adding "iexact".

I expected if json field is {"hello": "world"},
objects.filter(extra_data__hello="world") or
objects.filter(extra_data__hello=some_variable) should return this object
in query set, but it doesn't work.
objects.filter(extra_data__hello__iexact="world") fix this problem.

Here are queries example:
{{{
>>> token='world'
>>>
str(UserSocialAuth.objects.filter(extra_data__hello__exact=token).query)
'SELECT "social_auth_usersocialauth"."id",
"social_auth_usersocialauth"."user_id",
"social_auth_usersocialauth"."provider",
"social_auth_usersocialauth"."uid",
"social_auth_usersocialauth"."extra_data",
"social_auth_usersocialauth"."created",
"social_auth_usersocialauth"."modified" FROM "social_auth_usersocialauth"
WHERE ("social_auth_usersocialauth"."extra_data" -> hello) =
\'"\\"world\\""\''
>>>
str(UserSocialAuth.objects.filter(extra_data__hello__iexact=token).query)
'SELECT "social_auth_usersocialauth"."id",
"social_auth_usersocialauth"."user_id",
"social_auth_usersocialauth"."provider",
"social_auth_usersocialauth"."uid",
"social_auth_usersocialauth"."extra_data",
"social_auth_usersocialauth"."created",
"social_auth_usersocialauth"."modified" FROM "social_auth_usersocialauth"
WHERE UPPER(("social_auth_usersocialauth"."extra_data" ->> hello)::text) =
UPPER(world)'
}}}

Could you give me advice how to fix this issue?
Thank you

--
Ticket URL: <https://code.djangoproject.com/ticket/34548>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 8, 2023, 3:45:29 PM5/8/23
to django-...@googlegroups.com
#34548: Queryset JsonField filtering doesn't work as expected
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(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
-------------------------------------+-------------------------------------

Old description:

New description:

--

Comment (by Michael):

code fixes

--
Ticket URL: <https://code.djangoproject.com/ticket/34548#comment:1>

Django

unread,
May 8, 2023, 10:04:25 PM5/8/23
to django-...@googlegroups.com
#34548: Queryset JsonField filtering doesn't work as expected
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* status: new => closed
* resolution: => invalid


Comment:

Thanks for the report, however I cannot replicate the issue described.

If you need support in debugging your issue please refer to one of the
support channels for assistance: https://www.djangoproject.com/community/

--
Ticket URL: <https://code.djangoproject.com/ticket/34548#comment:2>

Reply all
Reply to author
Forward
0 new messages