Opened 4 years ago

Closed 4 years ago

#32078 closed Uncategorized (worksforme)

Inconsistent JSONField filtering for value None

Reported by: fre-db Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: JSONField, None
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When a JSONField has a key with Value None , only filtering on __key__in=[None] works, while you'd expect you could also just filter on __key=None or __key__isnull=True.

Example below:

import django
print(django.__version__)
3.1.2
print(Model.objects.get(pk=99657).jsonfield)
{'key': None}
print(Model.objects.filter(jsonfield__key=None).filter(pk=99657).count())
0
print(Model.objects.filter(jsonfield__key__is_null=True).filter(pk=99657).count())
0
print(Model.objects.filter((jsonfield__key__in=[None]).filter(pk=99657).count())
1

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: worksforme
Status: newclosed

It works as expected for me:

>>> NullableJSONModel.objects.create(value={'key': None})
NullableJSONModel object (14)
>>> NullableJSONModel.objects.filter(value__key=None)
<QuerySet [<NullableJSONModel: NullableJSONModel object (14)>]>
>>> NullableJSONModel.objects.filter(value__key__isnull=True)  # This will return also JSONFields without the `key`.
<QuerySet [<NullableJSONModel: NullableJSONModel object (14)>]>
>>> NullableJSONModel.objects.filter(value__key__in=[None])
<QuerySet []>

See Storing and querying for None and Key, index, and path transforms docs.

Note: See TracTickets for help on using tickets.
Back to Top