[Django] #33785: Broken __in lookup for keys of json field

13 views
Skip to first unread message

Django

unread,
Jun 14, 2022, 9:04:33 AM6/14/22
to django-...@googlegroups.com
#33785: Broken __in lookup for keys of json field
-------------------------------------+-------------------------------------
Reporter: Maksim | Owner: nobody
Iakovlev |
Type: Bug | Status: new
Component: Database | Version: 3.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 |
-------------------------------------+-------------------------------------
For example I have a model with json field:

{{{
class MyModel():
model_field = JsonField()
}}}

I create 3 records in db:

{{{
x1 = MyModel(model_field={json_field=''}).save()
x2 = MyModel(model_field={json_field=None}).save()
x3 = MyModel(model_field={json_field='foobar'}).save()
}}}

And then I want for filter all records which contains non-empty json_field
like this:

{{{
MyModel.objects.exclude(model_field__json_field__in=['', None])
}}}

I expect only x3 record in result, but x2 presents too. This is also
wrong:

{{{
MyModel.objects.exclude(model_field__json_field='').exclude(model_field__json_field__isnull=True)
}}}

Only this filter works as expected:

{{{
MyModel.objects.exclude(model_field__json_field='').exclude(model_field__json_field=None)
}}}

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

Django

unread,
Jun 14, 2022, 12:02:01 PM6/14/22
to django-...@googlegroups.com
#33785: Broken __in lookup for keys of json field
-------------------------------------+-------------------------------------
Reporter: Maksim Iakovlev | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
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 Mariusz Felisiak):

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


Comment:

Replying to [ticket:33785 Maksim Iakovlev]:


> {{{
> MyModel.objects.exclude(model_field__json_field__in=['', None])
> }}}
>
> I expect only x3 record in result, but x2 presents too.

This is a duplicate of #20024.

> This is also wrong:
>
> {{{
>
MyModel.objects.exclude(model_field__json_field='').exclude(model_field__json_field__isnull=True)
> }}}

It's [https://docs.djangoproject.com/en/stable/topics/db/queries/#key-
index-and-path-transforms documented] that `__isnull` on key transforms
are using ''"To query for missing keys"'', not to query for a `None`
value.

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

Reply all
Reply to author
Forward
0 new messages